【问题标题】:How to scroll to a specific object using scrollTo() in ScrollView?如何在 ScrollView 中使用 scrollTo() 滚动到特定对象?
【发布时间】:2017-07-19 16:50:28
【问题描述】:

我渲染了几个部分,其中一个部分是 ScrollView。这个 ScrollView 是一个水平日历,每个项目都是一个renderRow(),并且将 View TextButton 包裹在一个 View 中:

render()
{
    //..
    let day = this.renderDaysView();

    return(
        <View style={{ backgroundColor: 'white' }}>
            {day}
        </View>
    )
}

renderDaysView()
{
    let renderRow = (rowData, rowID) => {   
        return (
            <View key={rowID}>
                <TouchableOpacity ... >
                    <View ... >
                        <Text ... >{currentWeekDay}</Text>
                    </View>
                </TouchableOpacity>
            </View>
        );
    }

    return (
        <ScrollView ref={(scrollview) => this._daysScrollView = scrollview} horizontal= 'true'> 
        {
            this.state.Days.map((item, index) => renderRow(item, index) )
        }
        </ScrollView>
    }
}

现在,当屏幕显示时,我希望今天正好在屏幕中间。

跟随DocsscrollTo()可以带object

scrollTo( y? , object , x?, 动画?)

目前我正在使用这个技巧(缺少一个部分),我将当前日期乘以特定值:

componentWillUpdate(newProps: Object, newState: Object) {

    this._daysScrollView.scrollTo(
        {
            x:  // if first week, do nothin
                newProps.currentDate.getDate() < 6 ?
                    0
                :   // elseif if last week, scroll to scrollView width (missing)
                    newProps.currentDate.getDate() > 27 ? 
                        (newProps.currentDate.getDate() * 55 ) - ( 7 * 52) // TODO: should be scrollview width
                :   // else someday later, put in in the middle
                    (newProps.currentDate.getDate() - 4) * 55, 

            animated: true
        }
    )
}

所以我要么需要知道 _daysScrollView 的宽度,要么跳转到那个对象 - 4(保持在中间)

如果我的对象是组件,如何跳转到特定对象?(例如,如果今天是 19 号,则转到索引 19)

或者为了我的黑客,

我怎样才能获得_daysScrollView 的宽度?_daysScrellView.width)不起作用 谢谢

【问题讨论】:

    标签: javascript reactjs react-native scrollview


    【解决方案1】:

    要使用 scrollTo 方法,您必须获取要滚动到的元素的 x/y 位置。一个更简单的解决方案可能是这样的:

    componentDidUpdate() {
        let location = window.location;
        let dayOfMonth = new Date.getDate();
        location.hash = `#${dayOfMonth}`;
    }
    

    new Date.getDate(); 将根据月份中的哪一天返回一个数字 1 - 31。因此,如果您将日历中每一天的 id 设置为与该月的某一天相对应的数字。以上将滚动到它。

    location.hash = `#${dayOfMonth}`;
    

    然后将文档滚动到具有该 id 属性的元素。

    将每一天的 id 设置为它在月份中的数字位置,并将 window.location.hash 设置为路由到页面中当天的锚点。

    编辑:尝试用 componentDidUpdate 替换您的 componentWillUpdate() 方法。

    【讨论】:

    • 我说的是dom元素的id属性。即&lt;div id="_this_is_an_id" /&gt; 如果每个月的每一天都有一个符合以下模式的 id:july 1st id="1" july 2nd id="2" etc...那么提供的方法将满足您的需求。跨度>
    • 您声明了 dayOfMonth 吗?这段代码在我测试过的组件中完美运行。我的错误是,componentWillUpdate 应该是 componentDidUpdate 才能正常工作。对不起!我刚刚在一个工作应用程序中对其进行了测试,它按预期执行。滚动到具有该 ID 的元素。
    • 我没有声明dayOfMonth,因为我已经将它作为this.state.currentDay,它是一个Date 对象,并且由于某种原因它没有工作,直到我用new Date() 再次包装它.谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-12
    • 2017-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-19
    相关资源
    最近更新 更多