gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 17:56:10 24 4
gpt4 key购买 nike

在 web 的 javascript 中,我会做这样的事情来确定用户是否在可滚动页面/文档的顶部:

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

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

最佳答案

使用 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,
}
});

希望对您有所帮助!

关于javascript - React Native 判断用户是否在屏幕顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38893735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com