Skip to content

Instantly share code, notes, and snippets.

@UziTech
Created March 13, 2015 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save UziTech/6bf3c1a3de4e09226db1 to your computer and use it in GitHub Desktop.
Save UziTech/6bf3c1a3de4e09226db1 to your computer and use it in GitHub Desktop.
jQuery plugin to check if a point on the page is within an element.
/*
* DWTFYW License
*
* Author: Tony Brix, http://tonybrix.info
*
* Check if a point is within an element.
* Useful for mouseover on absolutely positioned overlapping elements.
*
* Example:
* $(document).mousemove(function(e){
* if($("#element").isOver(e.pageX, e.pageY)){
* alert("Mouse is over element.");
* }
* });
*/
(function ($) {
$.fn.isOver = function (x, y) {
var result = false;
this.each(function () {
var $this = $(this);
var top = $this.offset().top;
var bottom = $this.outerHeight() + top;
var left = $this.offset().left;
var right = $this.outerWidth() + left;
result = (x > left && x < right && y < bottom && y > top);
if (result) {
return false;
}
});
return result;
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment