简体   繁体   English

pointer-events: 没有允许滚动但禁用 click-JS/CSS

[英]pointer-events: none allow scroll but disable click-JS/CSS

Is there a way where setting pointer-events: none can only disable the click on that container but have scroll enable on the same with an overlay.有没有一种设置pointer-events: none只能禁用对该容器的点击,但可以在覆盖层上启用滚动。

I have fiddle: https://jsfiddle.net/1eu6d3sq/1/我有小提琴: https://jsfiddle.net/1eu6d3sq/1/

When I mask, using pointer-events: none , the entire container disables and when I remove this property- all events get enabled.当我使用pointer-events: none屏蔽时,整个容器将禁用,当我删除此属性时,所有事件都将启用。 css: css:

   .parent {
  position: relative;
  pointer-events: none; /* if I remove this the scrolling works and click is also enabled. if I add this, scrolling and click both get disabled */
}
    
.button {cursor: pointer}
.main {    
  height: 80px;
  overflow: auto;
}
    
.mask {
  z-index: 1000;
  pointer-events: none;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  cursor: default;
  height: 100%;
}

HTML: HTML:

 <div class="parent"><div class="main">

      <div class="aaaa">
      Some masked contentThis is some text covered by the maskSome contents above the maskSome contents above the maskSome contents above the maskSome contents above the maskSome ontents above the maskSome ontents above the maskSome ontents above the maskSome ontents aboveThis is some text covered by the maskSome contents above the maskSome contents above the maskSome contents above the maskSome contents above the maskSome ontents above the maskSome ontents above the maskSome ontents above the maskSome ontents aboveThis is some text covered by the maskSome contents above the maskSome contents above the maskSome contents above the maskSome contents above the maskSome ontents above the maskSome ontents above the maskSome ontents above the maskSome ontents above
      </div>
      <div>This is some text covered by the mask</div>
      <div>
      <span onclick="function hi(){alert('Hi!')};hi()">clicked SAPN</span>
      </div>
      <button class="button">
         <span class="span-button">OK</span>
      </button>
      <div class="mask" style="background-color: rgba(255, 255, 255, 0.7);">
        <div>
            <div>
              Some contents above the mask
            </div>
        </div>
      </div>
</div>
</div>

If I understand pointer-events property correctly, it will disable all events triggered.如果我正确理解pointer-events属性,它将禁用所有触发的事件。 Here I have added pointer-events to my parent/root element and all subsequent elements inside that parent gets disabled.在这里,我已将pointer-events添加到我的父/根元素,并且该父元素中的所有后续元素都被禁用。 however how can I make the parent/root DOM element listen only to scroll element?但是如何让父/根 DOM 元素只监听滚动元素?

I tried:我试过了:

window.addEventListener("scroll", this.onBodyScroll, true);

but onBodyScroll function never gets triggered.onBodyScroll function 永远不会被触发。

any ideas?有任何想法吗?

According toMDN documentation on pointer-events根据MDN关于pointer-events文档

Note that preventing an element from being the target of pointer events by using pointer-events does not necessarily mean that pointer event listeners on that element cannot or will not be triggered.请注意,通过使用指针事件防止元素成为指针事件的目标并不一定意味着该元素上的指针事件侦听器不能或不会被触发。 If one of the element's children has pointer-events explicitly set to allow that child to be the target of pointer events, then any events targeting that child will pass through the parent as the event travels along the parent chain, and trigger event listeners on the parent as appropriate.如果元素的一个子元素明确设置了指针事件以允许该子元素成为指针事件的目标,那么任何以该子元素为目标的事件都将在事件沿着父链传播时通过父元素,并触发事件监听器父母视情况而定。 Of course any pointer activity at a point on the screen that is covered by the parent but not by the child will not be caught by either the child or the parent (it will go "through" the parent and target whatever is underneath).当然,在屏幕上被父级覆盖但未被子级覆盖的点上的任何指针活动都不会被子级或父级捕获(它将 go “通过”父级并定位下面的任何内容)。

Keeping this in mind, re-setting pointer-events on your overflow container should enable scrolling while all other events disabled.请牢记这一点,在您的溢出容器上重新设置pointer-events应该启用滚动,同时禁用所有其他事件。 (Forgetting mask ) (忘记mask

 .main { position: relative; /* if I remove this the scrolling works and click is also enabled. if I add this, scrolling and click both get disabled */ pointer-events: none; }.button { cursor: pointer }.aaaa { height: 80px; overflow: auto; pointer-events: auto; } /*.mask { z-index: 1000; pointer-events: none; position: absolute; top: 0; left: 0; width: 100%; cursor: default; height: 100%; } */
 <div class="main"> <div class="aaaa"> Some masked contentThis is some text covered by the maskSome contents above the maskSome contents above the maskSome contents above the maskSome contents above the maskSome ontents above the maskSome ontents above the maskSome ontents above the maskSome ontents aboveThis is some text covered by the maskSome contents above the maskSome contents above the maskSome contents above the maskSome contents above the maskSome ontents above the maskSome ontents above the maskSome ontents above the maskSome ontents aboveThis is some text covered by the maskSome contents above the maskSome contents above the maskSome contents above the maskSome contents above the maskSome ontents above the maskSome ontents above the maskSome ontents above the maskSome ontents above </div> <div>This is some text covered by the mask</div> <div> <span onclick="function hi(){alert('Hi;')}:hi()">clicked SAPN</span> </div> <button class="button"> <span class="span-button">OK</span> </button> <,-- <div class="mask" style="background-color, rgba(255, 255. 255; 0.7);"> <div> <div> Some contents above the mask </div> </div> </div> --> </div>

Now if you want to mask , you don't really have to set any pointer-events .现在如果你想mask ,你真的不必设置任何pointer-events It should work as intended(click disabled and scroll enabled).它应该按预期工作(单击禁用并启用滚动)。

 .main { position: relative; /* if I remove this the scrolling works and click is also enabled. if I add this, scrolling and click both get disabled */ pointer-events: none; }.button { cursor: pointer }.aaaa { height: 80px; overflow: auto; pointer-events: auto; }.mask { z-index: 1000; position: absolute; top: 0; left: 0; width: 100%; cursor: default; height: 100%; }
 <div class="main"> <div class="aaaa"> Some masked contentThis is some text covered by the maskSome contents above the maskSome contents above the maskSome contents above the maskSome contents above the maskSome ontents above the maskSome ontents above the maskSome ontents above the maskSome ontents aboveThis is some text covered by the maskSome contents above the maskSome contents above the maskSome contents above the maskSome contents above the maskSome ontents above the maskSome ontents above the maskSome ontents above the maskSome ontents aboveThis is some text covered by the maskSome contents above the maskSome contents above the maskSome contents above the maskSome contents above the maskSome ontents above the maskSome ontents above the maskSome ontents above the maskSome ontents above </div> <div>This is some text covered by the mask</div> <div> <span onclick="function hi(){alert('Hi;')}:hi()">clicked SAPN</span> </div> <button class="button"> <span class="span-button">OK</span> </button> <div class="mask" style="background-color, rgba(255, 255, 255. 0;7);"> <div> <div> Some contents above the mask </div> </div> </div> </div>

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

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