views:

274

answers:

2

This Jquery problem has been bugging me for a while now. I developed a script, with one function detecting when the mouse leaves via the top of the page. Here is the code:

    $(document).bind("mouseleave", function(e)
    {
    console.log(e.pageY);
    if (e.pageY <= 1)
        {    
        now = new Date();           
        for (i=0; i < times.length; i++)
            {
            if (now.getTime() > times[i][0] && now.getTime() < times[i][1])
                {
                    $.fn.colorbox({iframe:true, width:650, height:600, href: "work.html", open: true});          
                }    
            }
        }
    });

This works perfectly for me in all browsers. For some reason it works randomly in Chrome and seemingly not at all in Firefox for a friend that tested the site. In my browser (firefox 3.5.3), e.pageY is logged in the console box as a number near 0, however in my friends browser (also firefox 3.5.3) the lowest value is around 240. I have no idea why this is happening considering identical browsers. Does anyone have a clue as to how to debug this, or another more reliable method to detect when the mouse goes out of the webpage via the top? I hope this makes sense.

A: 

I used another technic, almost works for all browsers. The trick is using $("body") or $(window).

$(window) do not work for IE, but $("body") works partially for FF as the body might not fill the whole window.

Here's the full page code:

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;&lt;script&gt;
  var mouseX = 0;
  var mouseY = 0;
  var theFrame;
$(function() {
    theFrame = $("body"); //$(window) for non-IE
      theFrame.mousemove( function(e) {
     //horizontal distance from edge
     mouseX = Math.min(theFrame.width() - e.pageX, e.pageX);
     //vertical distance from top
     mouseY = e.pageY;
     $("#mx").html(mouseX);
     $("#my").html(mouseY);
      });
    theFrame.mouseout(function() {
     if(mouseY<=mouseX)
      $("#in_out").html("out-top");
     else
      $("#in_out").html("out");
    });
    theFrame.mouseover(function() {
     $("#in_out").html("in");
    });
});
</script> 
</head>
<body>
<span id="in_out"></span>
<br />Hor: <span id="mx"></span>
<br />Ver: <span id="my"></span>

</body>
</html>
o.k.w
Note: in_out, mx, my are for debugging purpose only :P
o.k.w
+4  A: 

The problem appears if your window scrolls down, add a bunch of <br/>s to your page and scroll down one line and you'll see it.

So instead of looking to see if e.pageY <=1, subtract out the scrollTop:

if (e.pageY - $(window).scrollTop() <= 1)
    {    
    // do something
    }
fudgey
Thanks a lot, that fixed it
Lobe