Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/* coordinate-based click event
- jsbin http://jsbin.com/ivaxu3/edit
*/
$.fn.clickxy = function(xy, callback){
var inzone = false,
tolerance = 10;
return $(this)
.bind('mousemove',function(e){
var offset = $(this).offset(),
mouseX = e.pageX - offset.left,
mouseY = e.pageY - offset.top;
inzone = ( mouseX > (xy[0]-tolerance) && mouseX < (xy[0]+tolerance) && mouseY > (xy[1]-tolerance) && mouseY < (xy[1]+tolerance));
if(inzone){ $(this).addClass('xyhover'); }
else{ $(this).removeClass('xyhover'); }
})
.click(function(e){
if( inzone ){ callback(e); }
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.