javascript - React Native Webview - 如何实现滚动到顶部按钮?
问题描述
React Native WebView 似乎不支持scrollTo({x: 0, y: 0, animated: true})
并且不确定如何实现滚动到顶部按钮。
这就是我的 web 视图在渲染中的样子。
<WebView
source={{ uri: "https://google.com/" }}
refProps={(webView) => { this.webView.ref = webView; }}
/>
<TouchableOpacity onPress={() => this.webView.ref.scrollTo({x: 0, y: 0})} /> // How to make this work?
<Text>Scroll to Top</Text>
</TouchableOpacity>
此代码不起作用,因为scrollTo
React Native Webview 不支持。我该如何解决这个问题?
解决方案
const onPressButton = () => {
webViewRef.current.injectJavaScript(`
window.scroll({
top: 0,
left: 0,
behavior: 'smooth'
})
`);
};
您可以在按下按钮时使用此代码。
推荐阅读
- c++ - 在字符类型向量中使用数据成员函数“data()”
- google-chrome - 停止 Chrome 标签页在后台运行
- autocomplete - zsh 完成:按子字符串而不是前缀匹配
- c# - 在 ASP.NET Core Web 应用程序中使用 ADO.NET 大数据检索的性能
- haskell - 获取 :type-at 信息以获取 ghci 中的一长串模块
- javascript - 用于在特定时间切换 url 的 HTML 或 Javascript
- html - 具有独立水平移动容器的响应方块网格
- javascript - React 需要点击两次才能渲染从 api 获取的数据
- python - discord py bot 只看到一个成员
- laravel-5 - 如何从 Laravel 5.5 中控制器操作之外的任何功能注销