[英]Tracking a mousemove on overflowing content
考虑以下示例,其中覆盖放置在溢出内容的顶部:
window.addEventListener('DOMContentLoaded', () => { const overlay = document.getElementById('overlay'); overlay.addEventListener('mousemove', e => { alert("Fire callback with:", [e.clientX, e.clientY]); }); });
.container { width: 200px; height: 300px; outline: 1px solid black; position: relative; } .inner-container { height: 100%; overflow: auto; padding: 0 20px; } #overlay { position: absolute; left: 0; right: 0; top: 0; bottom: 0; background-color: rgba(255, 0, 0, 0.2); pointer-events: none; }
<div class="container"> <div class="inner-container"> <p>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.</p> </div> <div id="overlay"></div> </div>
我想要两件事:
但是,如您所见,由于pointer-events: none
,只有 #2 为真。
如果pointer-events: none
被删除,只有#1 变为真。
我怎样才能同时实现#1 和#2?
.inner-container 上的 Mousemove 看起来是简单的解决方案。 但如果你真的想使用叠加层,这也是一个解决方案。
window.addEventListener('DOMContentLoaded', () => { const overlay = document.getElementById('overlay'); document.addEventListener('mousemove', e => { const bounding = overlay.getBoundingClientRect(); const inVertical = e.clientY >= bounding.top && e.clientY <= (bounding.height + bounding.top); const inHorizontal = e.clientX >= bounding.left && e.clientX <= (bounding.width + bounding.left); if(inVertical && inHorizontal) { console.log("Fire callback with:", [e.clientX, e.clientY]); } }); });
.container { width: 200px; height: 300px; outline: 1px solid black; position: relative; } .inner-container { height: 100%; overflow: auto; padding: 0 20px; } #overlay { position: absolute; left: 0; right: 0; top: 0; bottom: 0; pointer-events: none; background-color: rgba(255, 0, 0, 0.2); }
<div class="container"> <div class="inner-container"> <p>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.</p> </div> <div id="overlay"></div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.