Created
July 27, 2014 04:06
-
-
Save hehongwei44/8d33a6e35ee045722e75 to your computer and use it in GitHub Desktop.
元素相对于整个文档的left和top的辅助函数
This file contains hidden or 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
| /*元素elem相对于整个文档的左端和顶端的位置*/ | |
| /** | |
| * 获取元素的水平位置 | |
| * */ | |
| function pageX(elem) { | |
| /** | |
| * 参看我们是否位于根元素 | |
| * 如果我们能继续得到上一个元素,增加当前偏移量并继续向下递归. | |
| * 否则,获取当前的偏移量. | |
| * */ | |
| return elem.offsetParent ? elem.offsetLeft + pageX(elem.offsetParent) : elem.offsetLeft; | |
| } | |
| /** | |
| * 获取元素的顶端位置 | |
| * | |
| * */ | |
| function pageY(elem) { | |
| /** | |
| * 参看我们是否位于根元素 | |
| * 如果我们能继续得到上一个元素,增加当前偏移量并继续向下递归. | |
| * 否则,获取当前的偏移量. | |
| */ | |
| return elem.offsetParent ? elem.offsetTop + pageY(elem.offsetParent) : elem.offsetTop; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment