Skip to content

Instantly share code, notes, and snippets.

@DarkStoorM
Last active August 29, 2015 14:08
Show Gist options
  • Save DarkStoorM/f9f15207c268f576edbc to your computer and use it in GitHub Desktop.
Save DarkStoorM/f9f15207c268f576edbc to your computer and use it in GitHub Desktop.
/*
JavaScript function to check whether if user clicked a specific region
on given element
Example <div>
<style>
#example {
width:500px;
height:500px;
background-color:#000;
}
</style>
We want to detect a click at (200,300), (200,300) - small rectangle
<div id="example"></div>
$("#example").click(function(e){
var something = ClickRegion(200,200,300,300, "#example", e);
if(something == true) {
alert("Clicked the proper region!");
}
});
*/
/*
following parameters:
ClickedRegion(x1, x2, y1, y2, DIV_CLASS_OR_ID, event)
*/
function ClickedRegion(x1, x2, y1, y2, str, e) {
var elem = document.getElementById(str);
x = e.pageX - elem.offsetLeft - $(str).offset().left;
y = e.pageY - elem.offsetTop - $(str).offset().top;
if((x >= x1 && x<=x2) && (y >= y1 && y<=y2)) {
return true;
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment