How to get maximum document scrolltop value

前端 未结 4 835
执笔经年
执笔经年 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:41

    If you want to "guess" the maximum scrolltop value, maybe you should compare the height of the document and the viewport height (size of your window).

    /**
     * Return a viewport object (width and height)
     */
    function viewport()
    {
        var e = window, a = 'inner';
        if (!('innerWidth' in window))
        {
            a = 'client';
            e = document.documentElement || document.body;
        }
        return { width : e[ a+'Width' ] , height : e[ a+'Height' ] }
    }
    
    // Retrieve the height of the document, including padding, margin and border
    var documentHeight = $(document).outerheight(true);
    var viewPortData = viewport();
    
    var maxScrollTop = documentHeight - viewPortData.height;
    

    Of course, in your plugin, you should also add a listener on the resize event, and re-calculate the maximum scrollTop.

提交回复
热议问题