views:

344

answers:

2

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

I have sort of got it working using some javascript (jQuery) on the hover event of the current post div. However, I've noticed the hover event only triggers when the mouse is actually moved. If the page is scrolled using the keyboard (page) up/down it does not trigger.

(I can note that soup.io for instance has found a way to get this working, but I can't find how they do it)

A: 

Do you have a sample? I'm guessing that the layout of the elements on the page are blocking the mouseover event. My simple example below works as you described it should. With the cursor at the top of the page and using keyboard navigation, the mouseover events are fired.

<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>
Scott Stevenson
This is similar to what I already have. It does not work on keyboard scrolling on Chrome, IE or Safari.
dsclementsen
+1  A: 

Unfortunately, it's quite complicated; you can no longer rely on the onMouseOver event - the only event that triggers when a page is scrolled is onScroll. The steps involved:

  1. Go through elements, storing each of their widths, heights and offsets (distance from left/top of screen) in an array.
  2. When the onScroll event is triggered check the last known position of the cursor against all chosen elements (go through the array) - if the cursor resides over one of the elements then call the handler.

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

J-P
I like this idea. I personally would only need heights and y offsets as the divs / posts are listed vertically. I'll give this a shot.
dsclementsen