简体   繁体   English

当光标位于固定 div 上时页面不滚动

[英]Page doesn't scroll when cursor is over fixed div

My layout looks almost identical to this codepen .我的布局看起来与这个 codepen几乎相同。

.parent {
  color: white;
  padding: 70px;
  position: relative;
  background-color: #0074d9;
  margin-top: 50px;
}

.element {
  background-color: lighten(#0074d9, 20);
  opacity: .85;
  padding: 20px;
  color: rgba(255,255,255,.9);
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
}

The codepen works the right way, so it's been hard to come up with a demonstrable example. codepen 以正确的方式工作,所以很难想出一个可证明的例子。

When my cursor is positioned over the fixed "child element" div, I want to be able to scroll the parent but not be able to clickthrough.当我的光标定位在固定的“子元素”div 上时,我希望能够滚动父元素但不能点击。

The common answer seems to be "pointer-events: none", but that allows click interaction with the page below.常见的答案似乎是“指针事件:无”,但这允许与下面的页面进行点击交互。

Open to other suggestions or explanations as to why it works in the codepen, but doesn't outside of it.关于它为什么在 codepen 中有效,但不在它之外的其他建议或解释。

The solution that worked for me was to use jquery to grab the parent by id and add my deltaY to its scrollTop.对我有用的解决方案是使用 jquery 通过 id 获取父级并将我的 deltaY 添加到其 scrollTop。

<div
    onWheel={(e) => {
        const component = $(`#content`);
        const contentScrollPosition = component.scrollTop();

        component.prop("scrollTop", contentScrollPosition + e.deltaY);
    }}
</div>

This allows me to scroll the parent even when my cursor is on the fixed-position div.即使我的光标位于固定位置的 div 上,这也允许我滚动父级。

Unfortunately, mobile doesn't work well.不幸的是,手机不能很好地工作。 First, you would need to track the touch event though onTouchStart, End, and Move.首先,您需要通过 onTouchStart、End 和 Move 跟踪触摸事件。 Even then, you lose touch scroll momentum which makes it feel too unnatural.即便如此,你也会失去触摸滚动的动力,这让它感觉太不自然了。

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

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