javascript - How can I make page scrolling trigger mouseover events? - Stack Overflow

When the mouse starts hovering over an element because of scrolling (either by wheel, or by keyboard sc

When the mouse starts hovering over an element because of scrolling (either by wheel, or by keyboard scrolling), it does not trigger a mouseover event on the elements it is hovering (Chrome 6 on OSX). What would be an elegant way to trigger the mouseover event for the correct elements when scrolling?

When the mouse starts hovering over an element because of scrolling (either by wheel, or by keyboard scrolling), it does not trigger a mouseover event on the elements it is hovering (Chrome 6 on OSX). What would be an elegant way to trigger the mouseover event for the correct elements when scrolling?

Share Improve this question edited Sep 3, 2022 at 7:18 Jasper de Vries 20.3k6 gold badges67 silver badges107 bronze badges asked Jul 27, 2010 at 6:24 Andrey FedorovAndrey Fedorov 9,70922 gold badges69 silver badges99 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

Honestly, this is gonna be a pain. You'll have to

  1. determine the size and position of every element that should get a mouseover handler.
  2. add a scroll listener to the window.
  3. In the handler, get the mouse cursor position and pageOffset.
  4. Find out which element(s) the cursor is in.
  5. manually call the actual mouseover handler
  6. (Find out which elements the cursor has left, if you want some mouseout behaviour too)

You may need to re-calculate the elements' positions and sizes if they are dynamic. (move 1. beneath 3.)

While this should work fine with block-level elements, I have absolutely no idea on a solution for inline elements.

This is much more simple in the modern day web using document.elementsFromPoint:

  1. Add a scroll listener to the window.
  2. In the handler, call document.elementsFromPoint.
  3. Manually call the actual pointerover handler for those elements. Keep track of these elements.
  4. (optionally) Manually call the actual pointermove handler for those elements.
  5. Check the list of elements from the previous time around. Manually call the actual pointerleave handler for elements no longer being hovered.

Here's some psuedo-code:

let prevHoveredEls = [];

document.addEventListener("scroll", (e) => {
  let hoveredEls = document.elementsFromPoint(e.pageX, e.pageY);
  hoveredEls = hoveredEls.filter(
    (el) => el.classList.contains("elements-cared-about")
  );
  const notHoveredEls = prevHoveredEls.filter(
    (el) => !prevHoveredEls.includes(el)
  );

  hoveredEls.forEach((el) => {
    const bcr = el.getBoundingClientRect();
    el.handlePointerEnter({
      layerX: e.pageX - bcr.left,
      layerY: e.pageY - bcr.top,
    });
  });

  notHoveredEls.forEach((el) => {
    const bcr = el.getBoundingClientRect();
    el.handlePointerLeave({
      layerX: e.pageX - bcr.left,
      layerY: e.pageY - bcr.top,
    });
  });

  prevHoveredEls = hoveredEls;
});

Try some hack like myDiv.style.opacity = 1+Math.random(); on scroll ;)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743743756a4499626.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信