getOffsetTop.js
Get an element’s distance from the top of the Document.
Works in all modern browsers, and at least back to IE9.
/*!
* Get an element's distance from the top of the Document.
* (c) 2017 Chris Ferdinandi, MIT License, https://gomakethings.com
* @param {Node} elem The element
* @return {Number} Distance from the top in pixels
*/
var getOffsetTop = function (elem) {
var location = 0;
if (elem.offsetParent) {
while (elem) {
location += elem.offsetTop;
elem = elem.offsetParent;
}
}
return location >= 0 ? location : 0;
};