【问题标题】:Strange horizontal scrolling in Android browserAndroid浏览器中奇怪的水平滚动
【发布时间】:2014-10-22 12:23:38
【问题描述】:

在 Android 设备上查看以下演示:

Scrolling Demo

屏幕外有一个红色框。当垂直间隔不存在时,您不能向任何方向拖动页面。当存在分隔符并占用比窗口更多的垂直空间时,您可以向下拖动页面(如预期的那样),但是现在您也可以水平拖动。

这似乎只发生在 Android 浏览器上。关于这里发生了什么的任何线索?我想完全阻止水平滚动,同时保留垂直滚动。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" id="viewportMobile" content="width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <link rel="stylesheet" type="text/css" href="test.css" />
    <style>
      html,body {
        overflow: hidden;
      }
      body {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        overflow-y: visible;
        overflow-x: hidden;
      }
      .offscreen {
        position: absolute;
        right: -20px;
        background-color: #ed0021;
        padding: 20px 20px 20px 20px;
      }
    </style>
    <script>
      var showSpacer=true;
      function toggleSpacer() {
        showSpacer = !showSpacer;
        var spacer = document.getElementById('spacer');
        spacer.style.display = showSpacer ? 'block' : 'none';
      }
    </script>

  </head>
  <body>
    <div class="toggle-button" onClick="toggleSpacer()">Toggle Spacer</div>
    <div class="offscreen"></div>
    <div id="spacer" style="width:50px; height:2000px; background-color:#444">
    </div>
  </body>
</html>

【问题讨论】:

    标签: android html css mobile-chrome


    【解决方案1】:

    删除溢出属性并将位置更改为 body 元素的相对位置对我有用。它应该是这样的:

    body {
        position: relative;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
      }
    

    编辑:错误的复制/粘贴

    编辑 2:更新答案

    【讨论】:

    • 对不起,我想保留垂直滚动但阻止水平滚动。我将编辑我的问题。
    • 如果您将位置更改为“相对”,您将获得所需的效果。我已经更新了我的答案。
    • 感谢标记,但这些参考文献都没有提到溢出,更不用说解释上述现象了。
    • 定位最终是导致此问题的原因。如果它最初是相对定位的,您可能没有看到它。无论如何溢出都可以工作,但.offscreen 元素仍然会被截断。如果您再次看到这种情况,请尝试使用 position 属性,直到获得所需的效果。祝你好运!
    猜你喜欢
    • 2018-03-23
    • 1970-01-01
    • 2022-11-11
    • 2016-11-16
    • 2014-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多