跟踪内容溢出时的鼠标移动

时间:2020-02-04 10:04:13

标签: javascript css overflow overlay

考虑以下示例,其中将叠加层放置在溢出的内容之上:

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>

我想要两件事:

  1. 当鼠标移动到叠加层上方时,我想通过触发带有鼠标坐标的回调来了解它。
  2. 溢出的内容应该是可滚动的。

但是,如您所见,由于pointer-events: none,只有#2是正确的。

如果pointer-events: none被删除,则只有#1变为真。

我如何同时获得#1和#2?

1 个答案:

答案 0 :(得分:1)

.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>