Chrome和Firefox之间的Flexbox/position滚动差异

3
我有一个固定的覆盖层,在里面填充了导航和内容,如在此例中所示: https://jsfiddle.net/9871Lz1u/1/ 我在一个固定位置的div中使用flexbox来控制导航按钮的位置,并使用相对定位的div来容纳内容。在Chrome中,这正是我想要的效果。然而,在Firefox / Safari(最新版本)中,当将光标悬停在flexbox(红色和蓝色)区域时,滚动被锁定。我必须将光标悬停在白色内容框上才能“启用”滚动。
我希望Chrome的行为成为最终一致的行为,无论光标在哪里都可以滚动。如果可能的话,我希望解决方案只使用纯HTML / CSS。
我的系统:macOS Version 10.13.2,Chrome Version 63.0.3239.132,Firefox Version 58.0

.container {
  background: black;
  display: block;
  position: fixed;
  top: 0px;
  left: 0px;
  right: 0px;
  bottom: 0px;
  overflow: auto;
}

.content {
  background: white;
  margin: auto;
  max-width: 400px;
  padding: 15px;
  z-index: 2;
  position: relative;
 overflow: auto;
}

.nav-outer {
  position:fixed;
 top: 0px;
 bottom: 0px;
 left: 0px;
 right: 0px;
  z-index: 1;
}

.nav-inner {
  display: flex;
 width: 100%;
 height: 100%;
 flex-direction: row;
 flex-wrap: nowrap;
}

.nav-center {
  flex: 1 100%;
 order: 2;
 max-width: 400px;
}

.nav-container {
  flex: 1 auto;
 display: -ms-flexbox;
 display: -webkit-flex;
 display: flex;
 /* -ms-flex-align: center;
 -webkit-align-items: center;
 -webkit-box-align: center; */
 align-items: center;
 overflow: hidden;
}

.nav-left {
  order: 1;
  background: red;
}

.nav-right {
  order: 3;
  background: blue;
}

.nav-button {
  width: 70px;
 height: 70px;
 border-radius: 50%;
 background-color: black;
 display: block;
 z-index: 10;
 margin: auto;
 background-repeat: no-repeat;
 background-size: 40px 40px;
 border: 1px solid white;
}
<div class="container">
  <div class="content">
    Some overflowable content<br/>
    Some overflowable content<br/>
    Some overflowable content<br/>
    Some overflowable content<br/>
    Some overflowable content<br/>
    Some overflowable content<br/>
    Some overflowable content<br/>
    Some overflowable content<br/>
    Some overflowable content<br/>
    Some overflowable content<br/>
    Some overflowable content<br/>
    Some overflowable content<br/>
    Some overflowable content<br/>
    Some overflowable content<br/>
    Some overflowable content<br/>
    Some overflowable content<br/>
    Some overflowable content<br/>
    Some overflowable content<br/>
    Some overflowable content<br/>
    Some overflowable content<br/>
    Some overflowable content<br/>
    Some overflowable content<br/>
    Some overflowable content<br/>
    Some overflowable content<br/>
    Some overflowable content<br/>
    Some overflowable content<br/>
    Some overflowable content<br/>
  </div>
  <div class="nav-outer">
    <div class="nav-inner">
      <div class="nav-container nav-left">
        <div class="nav-button"></div>
      </div>
      <div class="nav-center"></div>
      <div class="nav-container nav-right">
        <div class="nav-button"></div>
      </div>
    </div>
  </div>
</div>


1
请确保fiddle中的输出窗口足够短,以便产生溢出,并且足够宽以便您可以看到红蓝条。在Chrome中滚动滚轮时,在彩色条上悬停光标应该会滚动内容div,但是在Firefox中不会。 - CaptainStiggz
1个回答

2
浏览器通常的处理方式是,它们会滚动具有光标的元素。
即使在 Chrome 中,您也可以在“堆栈片段”(不处于“展开模式”时)中看到它,如果光标不在可滚动文本上,则会滚动整个 stackoverflow 页面,在“小提琴”中,如果您从红色或蓝色元素开始滚动,也会发生这种情况。
但是,如果您首先滚动文本,则 Chrome 将继续滚动它,即使将光标移动到红色或蓝色元素上,我的猜测是它记住了最后一个滚动的元素,保持具有滚动焦点的元素,直到另一个元素获得焦点。
因此,使用现有的标记需要编写脚本来实现这一点,以下是一个使用 jQuery 的示例:
通过标记语言的更改,您可以不需要脚本就可以去除容器,只需删除container即可。
这不会对布局产生影响,因为body的行为与固定的container几乎相同,因此它将是body滚动,并且可以跨浏览器工作。 更新后的示例 堆栈片段

body {
  background: black;
  margin: 0;
}

.content {
  background: white;
  margin: auto;
  max-width: 400px;
  padding: 15px;
  z-index: 2;
  position: relative;
  overflow: auto;
}

.nav-outer {
  position: fixed;
  top: 0px;
  bottom: 0px;
  left: 0px;
  right: 0px;
  z-index: 1;
}

.nav-inner {
  display: flex;
  width: 100%;
  height: 100%;
  flex-direction: row;
  flex-wrap: nowrap;
}

.nav-center {
  flex: 1 100%;
  order: 2;
  max-width: 400px;
}

.nav-container {
  flex: 1 auto;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  /* -ms-flex-align: center;
 -webkit-align-items: center;
 -webkit-box-align: center; */
  align-items: center;
  overflow: hidden;
}

.nav-left {
  order: 1;
  background: red;
}

.nav-right {
  order: 3;
  background: blue;
}

.nav-button {
  width: 70px;
  height: 70px;
  border-radius: 50%;
  background-color: black;
  display: block;
  z-index: 10;
  margin: auto;
  background-repeat: no-repeat;
  background-size: 40px 40px;
  border: 1px solid white;
}
<div class="content">
  Some overflowable content
  <br/> Some overflowable content
  <br/> Some overflowable content
  <br/> Some overflowable content
  <br/> Some overflowable content
  <br/> Some overflowable content
  <br/> Some overflowable content
  <br/> Some overflowable content
  <br/> Some overflowable content
  <br/> Some overflowable content
  <br/> Some overflowable content
  <br/> Some overflowable content
  <br/> Some overflowable content
  <br/> Some overflowable content
  <br/> Some overflowable content
  <br/> Some overflowable content
  <br/> Some overflowable content
  <br/> Some overflowable content
  <br/> Some overflowable content
  <br/> Some overflowable content
  <br/> Some overflowable content
  <br/> Some overflowable content
  <br/> Some overflowable content
  <br/> Some overflowable content
  <br/> Some overflowable content
  <br/> Some overflowable content
  <br/> Some overflowable content
  <br/>
</div>
<div class="nav-outer">
  <div class="nav-inner">
    <div class="nav-container nav-left">
      <div class="nav-button"></div>
    </div>
    <div class="nav-center"></div>
    <div class="nav-container nav-right">
      <div class="nav-button"></div>
    </div>
  </div>
</div>


1
你的第一个建议起了作用。我本来希望不绑定任何滚轮事件就能解决问题,但没有这样的运气。(正如我在问题中提到的那样,所有这些都发生在模态覆盖层中,因此固定容器是必要的)。此外,如果有人像我一样使用React,我最终在nav-outer元素上执行了onWheel = {this.onWheel.bind(this)},然后使用e.deltaY手动调整了content元素的滚动位置。 - CaptainStiggz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接