我有两个嵌套的div,它们都是position: fixed.外部div保存文本内容,并使用定义的高度和overflow-y: auto在内部滚动.内部div表示包含表示操作的链接的弹出菜单.弹出菜单向右偏移,因此它与Content-div及其滚动条略有重叠.

当我在内部div中的链接上悬停(或想要点击)时,它会按预期工作,除非鼠标移到链接的一部分,该部分也悬停在弹出菜单div后面呈现的滚动条上.

annotated screenshot of the problem

codepen个数字说明了这个问题.

我可能会更改HTML,但我更喜欢使用css的解决方案,它允许我将鼠标悬停在标记的位置上,并仍然"激活"链接,而不是可视化地位于div后面的滚动条.

推荐答案

你不能做你想做的事情,如果你直接嵌套了固定的parent-child,它将不会工作

html的 struct 必须如下所示

<div class="wrapper"> <<<<< this is fixed
   <div class="internal-scroll"> <<<<< this is relative
       <div class="overlay"> <<<<<< this is fixed
          <a>hover me, where the scrollbar is</a>
      </div>
   </div>
</div>

.wrapper {
  position: fixed;
  // add top, right, bottom, left
}

.internal-scroll {
  top: 5px;
  left: 5px;
  width: 200px;
  height: 200px;
  overflow-y: auto;
  border: LightCoral 2px solid;
}

.overlay {
  position: fixed;
  top: 40px;
  left: 160px;
  width: 100px;
  padding: 5px;
  background: aliceblue;
  border: solid 2px SlateBlue;
  z-index: 999;
  
}

.overlay a:hover {
  background: green;
  cursor: pointer;
}
<div class="wrapper">
  <div class="internal-scroll">
    <div class="overlay">
    <a>hover me, where the scrollbar is</a>
  </div>
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum
  
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum
  </div>
</div>

另外,请记住,固定元素不同于绝对元素,不要以任何上升relative的父元素作为定位参考,它们以视区为参考.所以实际上没有必要有fixed,parent-child个元素.

你可以把overlay放在internal-scroll之外,然后就会有同样的行为.

请参见示例:

.internal-scroll {
  position: fixed;
  top: 5px;
  left: 5px;
  width: 200px;
  height: 200px;
  overflow-y: auto;
  border: LightCoral 2px solid;
}

.overlay {
  position: fixed;
  top: 40px;
  left: 160px;
  width: 100px;
  padding: 5px;
  background: aliceblue;
  border: solid 2px SlateBlue;
  z-index: 999;
  
}

.overlay a:hover {
  background: green;
  cursor: pointer;
}
<div class="internal-scroll">
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum
  
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum
</div>
<div class="overlay">
    <a>hover me, where the scrollbar is</a>
</div>

Html相关问答推荐

HTML横幅未正确对齐页面顶部

垂直页眉,每行只显示一个使用css的字母

使用css第n个子元素,如何迭代Y组中的X个元素?

让多对图像在各自的div中叠加

IFRAME中的Chrome图像未调整大小

Style=背景图像URL引用变成&;Quot;

使具有FLEX父项的溢出自动的子元素收缩

与窗高匹配的响应式正方形网格

希望平稳过渡到悬停状态

如何使用 HTML 将对象数组发送到 Nodejs Express

Angular MatBadge 在高于 99 时不显示完整数字

如何使粘性导航栏能够跨其他组件工作?

不完整的悬停

如何并排放置部分?

重点/主动输入的概述问题

如何制作一个空的网格模板行来填充剩余空间?

为什么在使用src与srcdoc时 iframe 内容高度呈现不同?

浮动元素忽略 margin-top 属性后的块元素

样式不适用于来自 tailwindcss 的网络

无法在 css 的 body 标签中正确呈现(内联显示)