android - React Native Android WebView 如何禁用水平滚动?

标签 android css react-native webview

我在 webview 和滚动方面有问题。我想禁用所有滚动。我使用了 scrollEnabled,对于 IOS,它工作正常。但对于 android 来说,不存在这样的属性。我尝试了很多脚本,但没有任何效果。

乍一看一切正常,但如果用户向右滑动,文本/div 就会移出屏幕。

how it looks like when it is ok

事情就是这样

how it looks like when user will swipe to side

这是我的网页 View :

<WebView
  bounces={false}
  ref={(element) => { this.webViewRef = element; }}
  javaScriptEnabled
  scalesPageToFit={false}
  onShouldStartLoadWithRequest={this._onShouldStartLoadWithRequest}
  style={{
    height: this.state.webViewHeight,
  }}
  scrollEnabled={false}
  source={{
    html: `
          <!DOCTYPE html>
          <html>
            <head>${injectedStyle(platformStyles.fontSizes.base || 16, webViewWidth)}</head>
            <body>
              <div id="baseDiv" class="ex1">${content}</div>
              ${injectedJS}
            </body>
          </html>
    `,
    baseUrl: 'http://s3.eu-central-1.amazonaws.com',
  }}
  onNavigationStateChange={this._processNavigationStateChange}
  automaticallyAdjustContentInsets={false}
/>

我尝试添加这样的样式(overflow: 'hidden' 但它不起作用):

    const injectedStyle = (fontSize, widhtPlatform) => `<style type="text/css">
    ...
    body {
            display: flex;
            overflow: hidden;
            background-color: orange;
    ...
        }
    div.ex1 {
      overflow: hidden;
  background-color: coral;
  border: 1px solid blue;
    ...
    }
    </style>`;

和 JS 来禁用滚动:

const injectedJS = `
  <script>
    var scrollEventHandler = function()
      {
        window.scroll(0, window.pageYOffset)
      }
    document.addEventListener('scroll', scrollEventHandler, false);
    document.getElementById('scrollbar').style.display = 'block';
    document.body.style.overflow = 'hidden';

  </script>
`;

我们使用 RN:

react-native-cli: 2.0.1

react-native: 0.53.3

最佳答案

对于 Android,将 scalesPageToFit={false} 更改为 scalesPageToFit={true}。它会起作用。

// ....
const scalesPageToFit = Platform.OS === 'android';

// .... 
return 
    <WebView
      scalesPageToFit={scalesPageToFit}
      bounces={false}
      scrollEnabled={false}
// ....

更新:对于最新版本的 rn-webview,根据 html 内容,您可能还需要使用一些额外的 css 来定位有问题的内容:

最大宽度:100%;

overflow-x: hidden;

和/或showsHorizo​​ntalScrollIndicator={false}

关于android - React Native Android WebView 如何禁用水平滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49156152/

相关文章:

c# - Xamarin 应用程序 Release模式下的 NullReferenceException

javascript - react native : set TextInput value to 00:00

reactjs - 如何在渲染另一个组件后渲染一个 React Native 组件?

javascript - 如何使用 Javascript 将相应的图像插入到色样中?

java - 找不到将 CSS 导入 JavaFX 的源

android - 使用react-native-camera创建非常简单的应用程序时出错

java - 为什么我在这个演示中在 TensorFlowInferenceInterface 中获取了错误的结果?

android - 在 ViewPager 的一半处反转绘制顺序

android - 如何将 Google API 响应设置为 json

jquery - 如何在窗口大小调整时自动调整字体和图像(完整网站)的大小?