React-Native 自动向下滚动
React-Native automatically scroll down
我有一个应用程序,其中有一个 ScrollView 和一个按钮,里面有一个长文本:
return (
<ScrollView>
<Button onPress={slowlyScrollDown} title="Slowly scroll a bit down..." />
<Text>
[ Very long text here where user has to scroll ]
</Text>
</ScrollView>
)
当用户按下按钮时,我想慢慢向下滚动一点,这样他就可以看到
正文的前 5-10 行。
我会接受任何提供一段代码的答案,我可以如何实现它。
我不确定它是否有效,但这是方向:
const [offset,setOffset] = useState(0);
const scrollViewRef = useRef();
const slowlyScrollDown = () => {
const y = offset + 80;
scrollViewRef.current.scrollTo({x: 0, y, animated: true});
setOffset(y);
}
return (
<ScrollView ref={scrollViewRef} >
<Button onPress={slowlyScrollDown} title="Slowly scroll a bit down..." />
<Text>
[ Very long text here where user has to scroll ]
</Text>
</ScrollView>
)
我有一个应用程序,其中有一个 ScrollView 和一个按钮,里面有一个长文本:
return (
<ScrollView>
<Button onPress={slowlyScrollDown} title="Slowly scroll a bit down..." />
<Text>
[ Very long text here where user has to scroll ]
</Text>
</ScrollView>
)
当用户按下按钮时,我想慢慢向下滚动一点,这样他就可以看到 正文的前 5-10 行。
我会接受任何提供一段代码的答案,我可以如何实现它。
我不确定它是否有效,但这是方向:
const [offset,setOffset] = useState(0);
const scrollViewRef = useRef();
const slowlyScrollDown = () => {
const y = offset + 80;
scrollViewRef.current.scrollTo({x: 0, y, animated: true});
setOffset(y);
}
return (
<ScrollView ref={scrollViewRef} >
<Button onPress={slowlyScrollDown} title="Slowly scroll a bit down..." />
<Text>
[ Very long text here where user has to scroll ]
</Text>
</ScrollView>
)