Reputation: 3158
I know that I can track when the mouse is moved like this:
$("body").mousemove( function(e){
alert( "You moved to WIDTH " + e.pageX );
} );
Ok. I wanna be able to tell whether the mouse pointer was moved to left or to the right.
I know that to do this, I'll have to know the previous mouse position and simply compare it to the current. But how can I do this?
Upvotes: 2
Views: 4262
Reputation:
<script>var pos=0;
$("body").mousemove( function(e){
if(pos<e.pageX)
alert( "You moved to RIGHT" );
else alert("oved to left");
pos=e.pageX;
} );
Upvotes: 1
Reputation: 145368
var prevX = 0;
$(window).mousemove(function(e) {
$("div").text(prevX >= e.pageX ? "left" : "right");
prevX = e.pageX;
});
DEMO: http://jsfiddle.net/tb86F/
Upvotes: 6