跟踪溢出内容上的鼠标移动
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>
考虑以下示例,其中叠加层位于溢出内容之上:
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>