Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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