Last active
August 29, 2015 14:09
-
-
Save yuezk/f578f827be7e3c2fde1b to your computer and use it in GitHub Desktop.
得到元素相对于document的offset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 得到元素相对于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