【问题标题】:'overflow-x: scroll' inside a fixed div prevents vertical scrolling on ios固定 div 内的“溢出-x:滚动”可防止在 ios 上垂直滚动
【发布时间】:2020-10-22 00:03:04
【问题描述】:

似乎如果你有一个水平滚动的 div,在一个定位固定的 div 内,它会阻止 IOS 上的垂直滚动。 IE - 如果我通过将手指放在水平滚动 div 上开始滚动,并尝试垂直滚动,则没有任何反应。

在我的同事 Andriod 设备上似乎没问题。

我已经创建了一个测试用例,在这里演示了这个问题:

http://jsbin.com/jikatugeli/

这里是html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  some content underneath
  <div class="pop-up">
    <p>I'm some other content</p>
    <div class="scrollable">
      <div class="item">hi</div>
      <div class="item">hi</div>
      <div class="item">hi</div>
      <div class="item">hi</div>
      <div class="item">hi</div>
      <div class="item">hi</div>
      <div class="item">hi</div>
      <div class="item">hi</div>
      <div class="item">hi</div>
      <div class="item">hi</div>
      <div class="item">hi</div>
    </div>
    <div class="somemoretext">
      hello there, I am some text to make things scrollable
    </div>
  </div>
</body>
</html>

这是css

p {
  font-size:22px;
}

.item {
  display:inline-block;
  width:80px;
    height:60px;
  font-size:78px;
}

.scrollable {
  width:350px;
  white-space: nowrap;
  overflow-x:scroll;
  overflow-y:hidden;
}

.pop-up {
  position:fixed;
  height:300px;
  background:red;
  border: 1px solid #000;
  width:100%;
  top:0;
  overflow-y:scroll;
  z-index:9999;
}

.somemoretext {
  padding-top:600px;
}

感谢您的帮助。

【问题讨论】:

  • 我已经在我的 iPad 2 和 iPhone 7 上尝试过你的演示,但我找不到任何问题。
  • 我也有一部 iphone 7。我会尝试更具体地说明这个问题。请注意,如果您将手指放在红色上并尝试上下滚动,则效果很好。但是,如果您首先将手指放在“hi hi hi hi hi”(水平滚动的div)上,您就无法上下滚动。感谢您的帮助!

标签: html ios css


【解决方案1】:

下面的 css 解决了你的问题

html,body{height:100%}
body{background:red}
p {
  font-size:22px;
}

.item {
  display:inline-block;
  width:80px;
    height:60px;
  font-size:78px;
}

.scrollable {
  width:350px;
  white-space: nowrap;
  overflow-x:scroll;
  overflow-y:hidde;
  position: relative;
  -webkit-transform: translateZ(0);
}

.pop-up {
  position:fixed;
  height:300px;
  background:blue;
  border: 1px solid #000;
  width:100%;
  top:0;
  overflow-y:scroll;
  z-index:9999;
  -webkit-overflow-scrolling: touch;
}

.somemoretext {
  padding-top:600px;
}

包含 -webkit- 的行是在 Safari 中进行滚动工作的关键。
在 .pop-up DIV 中,您需要使用 overflow-y: scroll 和 –webkit-overflow-scrolling: touch 来强制滚动。在 .scrollable 你需要 –webkit-transform: tranzlateZ(0);上下移动实际的 html 内容,否则 DIV 将滚动但溢出的内容不会显示。

【讨论】:

  • 完美!效果很好。非常感谢!
  • 就我而言,我还必须将 -webkit-overflow-scrolling: touch; 添加到 body 元素,因为它也使用了 overflow-x:hidden 。希望它会为某人节省一些时间。
【解决方案2】:

只需将-webkit-overflow-scrolling: touch; 添加到正文样式即可为我解决此问题。

【讨论】:

    猜你喜欢
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-26
    相关资源
    最近更新 更多