Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

detect mouse direction

I am trying this code to detect if the mouse direction is going up or down:

<html>  
    <head></head>
    <body>
        <div style="width: 500px; height: 500px; background: red;"></div>
    </body>
</html>

var mY = 0;
$('body').mousemove(function(e) {
    mY = e.pageY;
    if (e.pageY < mY) {
        console.log('From Bottom');
        return;

    } else {
        console.log('From Top');
    }

});

However this code doesn't work was i expect. Console log always show "from top"

Any idea ?

demo

like image 581
daniel__ Avatar asked Dec 09 '11 18:12

daniel__


People also ask

How do I check mouse movement?

The Mouse Efficiency Test is a test to measure your speed and accuracy when moving your mouse. The rule is quite simple - just move the ball along the curve to the other end attached to it. Please make sure you're not moving out of the curve and be as fast as you can.

Why is my mouse moving the opposite way?

Sometimes, the reason is simple, though not that obvious – your mouse could start scrolling in the wrong direction because of accumulated dust around the scrolling wheel. Old batteries are another common culprit causing wireless mice to scroll the wrong way. However, most often, the problem lies in the mouse driver.

Can you change the mouse pointer direction?

Click 'Computer' in the bottom left of the screen. Click 'Control Center' from the pop up menu. In the 'Hardware' section of the Control Center, select'Mouse'. In the section entitled 'Mouse Orientation', make sure that the field 'Left Handed Mouse' has an 'x' in the box next to it.


1 Answers

var mY = 0;
$('body').mousemove(function(e) {

    // moving upward
    if (e.pageY < mY) {
        console.log('From Bottom');

    // moving downward
    } else {
        console.log('From Top');
    }

    // set new mY after doing test above
    mY = e.pageY;

});
like image 188
maček Avatar answered Sep 20 '22 22:09

maček