React Native 判断用户是否在屏幕顶部

React native determine if user is at the top of the screen

在网络中的 javascript 我会做这样的事情来确定用户是否在可滚动的顶部 page/document:

$(document).scroll(function() { 
   if($(window).scrollTop() === 0) {
     //do stuff
   }
});

React Native 中是否有类似的东西可以帮助我确定用户是否在屏幕顶部?

是的 ScrollView 组件有 onScroll 方法。 pageX 和 pageY 属性 evt nativeEvent contentOffset x y

handleScroll(event) {
 console.log(event.nativeEvent.contentOffset.y)
},

<ScrollView onScroll={this.handleScroll}/>

使用 ScrollView 你可以这样做:

_handleScroll(event) {
    if(event.nativeEvent.contentOffset.y <= 0) {
        console.log('top!');
    }
}
render() {
    return (
        <View style={styles.container}>
            <ScrollView 
                onScroll={this._handleScroll}     
                onScrollAnimationEnd={this._handleScroll} 
                style={{flex: 1}}>
                    <View style={[styles.child, {backgroundColor: 'red'}]} />
                    <View style={[styles.child, {backgroundColor: 'orange'}]} />
                    <View style={[styles.child, {backgroundColor: 'green'}]} />
            </ScrollView>
        </View>
    );
}
var styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#F5FCFF',
    },
    child: {
        flex: 1,
        height: 400,
    }
});

希望对您有所帮助!