Does anyone know a mouse trick / alternative that runs when scrolling with the keyboard

Is there an alternative method or trick to the hover method that can call a function when the cursor moves from one div to another when the user scrolls the page.

I have a view that works with javascript (jQuery) in the hover event of the current post div. However, I noticed that the hover event only fires when the mouse is actually moved. If a page scrolls up / down using the keyboard (page), it does not start.

(I can notice that soup.io, for example, found a way to make this work, but I cannot find how they do it)

+3
source share
3 answers

Unfortunately, this is rather complicated; you can no longer rely on an event - the only event that fires when a page scrolls is . Stages: onMouseOver onScroll

  • Go through the elements, saving each of their width, height and offset (distance to the left / top of the screen) in the array.
  • When the onScroll event is fired, check the last known cursor position for all selected elements (go through the array) - if the cursor is over one of the elements, call the handler.

Fast (unreliable) prototype: http://pastie.org/507589

+1
source

? , mouseover. , . , .

<html>
<body>
<script>
function log(text)
{
    document.getElementById('logger').value += text + "\n";
}
</script>

<div id="div1" style="background: yellow; height: 100px;margin-top: 100px" onmouseover="log('mouseover div1');">
div1
</div>
<textarea id="logger" cols="60" rows="12" style="float:right;"></textarea>
<div id="div2" style="background: red; height: 1000px" onmouseover="log('mouseover div2');">
div2
</div>
</body>
</html>
0

You are looking for the wheel event.

document.getElementById('myDiv').onmousewheel = function() {
  alert('You win!');
  alert('Seriously! It just like that');
};

I tested this only in Chrome (webkit)

0
source

Source: https://habr.com/ru/post/1710171/


All Articles