Skip to content

Instantly share code, notes, and snippets.

@yuezk
Last active August 29, 2015 14:09
Show Gist options
  • Save yuezk/f578f827be7e3c2fde1b to your computer and use it in GitHub Desktop.
Save yuezk/f578f827be7e3c2fde1b to your computer and use it in GitHub Desktop.
得到元素相对于document的offset
/**
* 得到元素相对于document的offset
* @param {Element} ele
* @return {object}
*/
function getOffset(ele) {
var box = {top: 0, left: 0};
var doc = ele.ownerDocument;
var docElem = doc.documentElement;
var win = doc.defaultView;
if (ele.getBoundingClientRect) {
box = ele.getBoundingClientRect();
}
return {
top: box.top + win.pageYOffset - docElem.clientTop,
left: box.left + win.pageXOffset - docElem.clientLeft
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment