Отслеживание движения мыши при переполнении контента - PullRequest
0 голосов
/ 04 февраля 2020

Рассмотрим следующий пример, где наложение размещается поверх переполненного содержимого:

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. Переполняемое содержимое должно быть прокручиваемым.

Однако, как вы можете видеть, только # 2 верно из-за pointer-events: none.

Если удалить pointer-events: none, становится верным только № 1.

Как я могу достичь и # 1, и # 2?

1 Ответ

1 голос
/ 04 февраля 2020

MouseMove на .inner-container выглядит простым решением. Но если вы действительно хотите использовать оверлей, это тоже решение.

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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...