Created
April 19, 2018 16:30
-
-
Save AeonFr/7d333a51cce958e5729d3105aab4a0e8 to your computer and use it in GitHub Desktop.
calculateTotalOffsetTop() & calculateTotalOffsetLeft() - Recursive totalOffset of an $element, to calculate it's position relative to the whole page.
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 calculateTotalOffsetTop (elem, total){ | |
total = total || 0; | |
total = elem.offsetTop + total; | |
if (elem.offsetParent) return calculateTotalOffsetTop(elem.offsetParent, total) | |
else return total; | |
} | |
function calculateTotalOffsetLeft (elem, total){ | |
total = total || 0; | |
total = elem.offsetLeft + total; | |
if (elem.offsetParent) return calculateTotalOffsetLeft(elem.offsetParent, total) | |
else return total; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
⭐ Usage:
var $selector = document.querySelector('#myDiv')
calculateTotalOffsetTop($selector)
Please notice: