javascript 获得元素坐标

var getCoords = function(el){
var box = el.getBoundingClientRect(),
isQuirk = (document.documentMode) ? (document.documentMode==5) ? true : false : ((document.compatMode=="CSS1Compat") ? false : true),
doc = el.ownerDocument,
body = doc.body,
html = doc.documentElement,
clientTop = html.clientTop || body.clientTop || 0,
clientLeft = html.clientLeft || body.clientLeft || 0,
top = box.top + (self.pageYOffset || !isQuirk && html.scrollTop || body.scrollTop ) - clientTop,
left = box.left + (self.pageXOffset || !isQuirk && html.scrollLeft || body.scrollLeft) - clientLeft;
return { top: top, left: left };
};

你可能感兴趣的:(JavaScript)