ios - React Native WebView 滚动行为无法按预期工作

标签 ios reactjs react-native scroll webview

我有一个使用 React Native 的原生应用 WebView包装网页。

      <WebView
        ref={webview => (this.webview = webview)}
        source={{
          uri: `http://localhost:8000`
        }}
      />

在此示例中,页面只是指向具有以下代码的 localhost 实例,该代码会在用户滚动时递增一个数字:

var i = 0;
var $el = document.getElementById('counter')

window.addEventListener('scroll', function(e) {
  e.preventDefault();
  i += 1;
  $el.innerText = i
});
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body style="height: 2000px; background: linear-gradient(dodgerblue, yellowgreen)">
<div id="type"></div>
<div id="counter" style="position: fixed">0</div>
</body>
</html>

React Native WebView 中的滚动行为与浏览器不同。它在触发 scroll 事件之前等待滚动完成。

这是行为的比较;在浏览器中,滚动事件会在滚动过程中触发,如预期的那样:

scroll-behaviour--browser

在 iOS 11 的 React Native 项目中,滚动事件在滚动完成后触发:

scroll-behaviour--simulator

这种行为大概是出于性能原因而使用的,但它几乎会破坏我的应用程序的预期行为。

还有其他我没有想到的解决方法吗?

如何确保滚动事件的行为方式与我的 React Native 应用程序中的浏览器行为方式相同?

最佳答案

我在 MDN scroll event documentation 上找到了以下信息.我认为问题与 react-native 无关。

Browser compatibility

iOS UIWebView

In iOS UIWebViews, scroll events are not fired while scrolling is taking place; they are only fired after the scrolling has completed. See Bootstrap issue #16202. Safari and WKWebViews are not affected by this bug.

关于ios - React Native WebView 滚动行为无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46541744/

相关文章:

ios:水平缩放字体而不光栅化

ios - iOS并发-未达到理论最大值附近的任何地方

ios - 如何将UIImage保存为SVG文件

javascript - 如何使用 React 启用/禁用自定义验证按钮

javascript - :before only working on last element

ios - 使用 "Next"作为返回键

javascript - 为什么 React-router 在调用 browserHistory.push 方法后不立即重定向?

javascript - 有条件地更改禁用的按钮样式

react-native - 使用 React-Navigation 时排毒测试挂起

javascript - 来自 react-native-elements 的样式卡 : Align Title and icon vertically