Created
October 27, 2012 06:24
-
-
Save zhuzhuaicoding/3963196 to your computer and use it in GitHub Desktop.
Updated "find position" script
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
| function findPos(obj) { | |
| var curleft = curtop = 0, scr = obj, fixed = false; | |
| while ((scr = scr.parentNode) && scr != document.body) { | |
| curleft -= scr.scrollLeft || 0; | |
| curtop -= scr.scrollTop || 0; | |
| if (getStyle(scr, "position") == "fixed") fixed = true; | |
| } | |
| if (fixed && !window.opera) { | |
| var scrDist = scrollDist(); | |
| curleft += scrDist[0]; | |
| curtop += scrDist[1]; | |
| } | |
| do { | |
| curleft += obj.offsetLeft; | |
| curtop += obj.offsetTop; | |
| } while (obj = obj.offsetParent); | |
| return [curleft, curtop]; | |
| } | |
| function scrollDist() { | |
| var html = document.getElementsByTagName('html')[0]; | |
| if (html.scrollTop && document.documentElement.scrollTop) { | |
| return [html.scrollLeft, html.scrollTop]; | |
| } else if (html.scrollTop || document.documentElement.scrollTop) { | |
| return [ | |
| html.scrollLeft + document.documentElement.scrollLeft, | |
| html.scrollTop + document.documentElement.scrollTop | |
| ]; | |
| } else if (document.body.scrollTop) | |
| return [document.body.scrollLeft, document.body.scrollTop]; | |
| return [0, 0]; | |
| } | |
| function getStyle(obj, styleProp) { | |
| if (obj.currentStyle) { | |
| var y = obj.currentStyle[styleProp]; | |
| } else if (window.getComputedStyle) | |
| var y = window.getComputedStyle(obj, null)[styleProp]; | |
| return y; | |
| } | |
| http://www.greywyvern.com/?post=331 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment