简体   繁体   English

将鼠标悬停在固定div上可防止在div内滚动

[英]Hovering fixed div prevents scroll inside div

Question: Is it possible to prevent this behaviour? 问题:是否可以防止这种行为?

What I tried: Changing Z-index and searching forums. 我尝试过的方法:更改Z-index和搜索论坛。 Challenge is prober key-words for prober search. 挑战是探针搜索的关键词。 Mostly get hits about preventing scroll behind fixed divs. 通常会获得有关防止固定div滚动的热门信息。 A few JS suggestions but all with flicker. 一些JS建议,但都带有闪烁感。

Real-world application: Nav/close not preventing scroll on hover. 实际应用:导航/关闭不会阻止悬停滚动。

HTML 的HTML

<div class="box">
    <div class="nav">nav (close)</div>
    More text in full example. (lorem ipsum)
</div>

CSS 的CSS

.nav {
    position: fixed;
    top: 80px;
    right: 80px;
    padding: 50px;
    background-color: pink;
    border: 3px solid red;
}

.box {
    position: fixed;
    top: 50px;
    right: 50px;
    left: 50px;
    bottom: 50px;
    border: 3px solid red;
    overflow: scroll;
}

https://jsfiddle.net/johnmichealsen/nyo5Lzck/8/ https://jsfiddle.net/johnmichealsen/nyo5Lzck/8/

How about using position sticky instead of fixed? 如何使用位置固定而不是固定位置? This would require an added container element of some sort wrapping the text content. 这将需要添加某种包装文本内容的容器元素。 https://jsfiddle.net/Lmp6bagq/ https://jsfiddle.net/Lmp6bagq/

.nav {
  position: sticky;
  top: 80px;
  right: 20px;
  float: right;
  width: 200px;
  padding: 50px;
  background-color: pink;
  border: 3px solid red;
  z-index: 1;
}


.content {
  position: absolute;
  width: 100%;
}

.box {
  position: fixed;
  top: 50px;
  right: 50px;
  left: 50px;
  bottom: 50px;
  border: 3px solid red;
  overflow: scroll;
}

Add this CSS styling to your fixed div to allow scrolling while hovering over the nav element. 将此CSS样式添加到固定的div中,以便在将鼠标悬停在nav元素上时进行滚动。

pointer-events: none;

The downside to this solution is that you'll no longer be able to click on the element to use it as a button. 该解决方案的缺点是您将不再能够单击该元素以将其用作按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM