Skip to content

Instantly share code, notes, and snippets.

@larscwallin
Created December 28, 2011 16:35
Show Gist options
  • Save larscwallin/1528591 to your computer and use it in GitHub Desktop.
Save larscwallin/1528591 to your computer and use it in GitHub Desktop.
jquery.elementFromPoint
(function ($){
var check=false, isRelative=true;
$.elementFromPoint = function(x,y)
{
if(!document.elementFromPoint) return null;
if(!check)
{
var sl;
if((sl = $(document).scrollTop()) >0)
{
isRelative = (document.elementFromPoint(0, sl + $(window).height() -1) == null);
}
else if((sl = $(document).scrollLeft()) >0)
{
isRelative = (document.elementFromPoint(sl + $(window).width() -1, 0) == null);
}
check = (sl>0);
}
if(!isRelative)
{
x += $(document).scrollLeft();
y += $(document).scrollTop();
}
return document.elementFromPoint(x,y);
}
})(jQuery);
@jaminellis
Copy link

Beware, in IE 8, you won't get == null, event if elementFromPoint is operating outside the window area, for isRelative will be incorrect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment