「JavaScript入門」要素の位置座標を取得するサンプルコード

JavaScriptコード:
var getCoords = function(el){
var box = el.getBoundingClientRect(),
cftKdf = (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 || !cftKdf && html.scrollTop || body.scrollTop ) – clientTop,
left = box.left + (self.pageXOffset || !cftKdf && html.scrollLeft || body.scrollLeft) – clientLeft;
return { top: top, left: left };
};

JavaScript

Posted by arkgame