Skip to content

Instantly share code, notes, and snippets.

@rask
Created April 26, 2016 14:26
Show Gist options
  • Save rask/0dbbd301345c07b83d0aa7b6a113d0dc to your computer and use it in GitHub Desktop.
Save rask/0dbbd301345c07b83d0aa7b6a113d0dc to your computer and use it in GitHub Desktop.

Revisions

  1. rask created this gist Apr 26, 2016.
    31 changes: 31 additions & 0 deletions document-scrolltop-shim.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    /**
    * 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;
    };