Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A somewhat cross-browser implementation of getting the current `scrollTop` value of the body/document element.
/**
* document-scrolltop-shim.js
*
* Cross-browser adjustments for getting and setting the current scrollTop -value for
* the body/html element.
*
* @author Otto Rask
*/
/**
* Get the scrollTop from either body or html element. As only one
* or the other is used in a browser, it returns zero (correct),
* or if either value is set to something else than 0 then the
* value.
*
* @return int
*/
var getDocumentScrollTop = function () {
return document.body.scrollTop || document.documentElement.scrollTop || 0;
};
/**
* Set the scrollTop value. Setting to both should work as browsers
* only use one or the other.
*
* @param int val
*/
var setDocumentScrollTop = function (val) {
document.body.scrollTop = val;
document.documentElement.scrollTop = val;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment