js 獲取座標下元素elementFromPoint
//考慮滾動條 及iframe
function GetDomByPosition(x,y) {
var sx =document.documentElement.scrollLeft;
var sy =document.documentElement.scrollTop;
var dom = document.elementFromPoint(x-sx, y-sy) ;
if (dom == null) {
return null;
}
var $dom =$(dom);
var DomName = $dom[0].tagName.toLowerCase();
if (DomName == 'html' || DomName == 'body') {
return null;
}
while (DomName == 'iframe' || DomName == 'frame')
{
var FLeft = Math.ceil($dom.offset().left);
var FTop = Math.ceil($dom.offset().top);
x = x - FLeft;
y = y - FTop;
// alert ('座標:'+ x+'*'+y );
var $dom=$($dom[0].contentWindow.document.elementFromPoint(x, y));
if ($dom[0]== null) {
return null;
}
DomName = $dom[0].tagName.toLowerCase();
if (DomName == 'html' || DomName == 'body') {
return null;
}
}
return $dom;
}