Created
April 26, 2016 14:26
-
-
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.
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
/** | |
* 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