I wanted to trigger an action with mouse move, but I didn't want the event to trigger if the mouse move is very small, here is the code I ended up with.

var iniX=0,iniY=0,triggerLength=150;

$(document).mousemove(function(e){

if(iniX==0&&iniY==0){

iniX=e.pageX;

iniY=e.pageY;

}

movementlength=Math.sqrt( Math.pow(Math.abs(iniX-e.pageX),2) + Math.pow(Math.abs(iniY-e.pageY),2) );

if(movementlength > triggerLength){

//code goes here

}

});

Can be done without jquery using

var iniX=0,iniY=0,triggerLength=150;

document.onmousemove = function(e){

if(iniX==0&&iniY==0){

iniX=e.pageX;

iniY=e.pageY;

}

movementlength=Math.sqrt( Math.pow(Math.abs(iniX-e.pageX),2) + Math.pow(Math.abs(iniY-e.pageY),2) );

if(movementlength > triggerLength){

//code goes here

}

}

### Related Posts by Categories

## 0 Responses:

Post a Comment