Skip to content

Instantly share code, notes, and snippets.

@rask
Created April 26, 2016 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rask/0dbbd301345c07b83d0aa7b6a113d0dc to your computer and use it in GitHub Desktop.
Save rask/0dbbd301345c07b83d0aa7b6a113d0dc to your computer and use it in GitHub Desktop.
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