Skip to content

Instantly share code, notes, and snippets.

@matthewpenkala
Forked from UziTech/jquery.isOver.js
Created July 26, 2023 14:17
Show Gist options
  • Save matthewpenkala/d83673b11939b49d4ef2a4126aec03ab to your computer and use it in GitHub Desktop.
Save matthewpenkala/d83673b11939b49d4ef2a4126aec03ab 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