How to get maximum document scrolltop value

前端 未结 4 822
执笔经年
执笔经年 2020-12-15 20:12

I am making some plugin and I need to get maximum scrollTop value of the document. Maximum scrollTop value is 2001 , but $(docum

4条回答
  •  [愿得一人]
    2020-12-15 20:25

    In vanilla Javascript I'm currently doing this:

    getScrollTopMax = function () {
      var ref;
      return (ref = document.scrollingElement.scrollTopMax) != null
          ? ref
          : (document.scrollingElement.scrollHeight - document.documentElement.clientHeight);
    };
    

    This returns Gecko's non-standard scrollTopMax if it exists, otherwise calculates it. I'm using document.scrollingElement here, which only exists on particular browsers, but it's easy to polyfill.

提交回复
热议问题