How do I get the scroll position of a document?

前端 未结 9 1739
面向向阳花
面向向阳花 2020-12-12 20:53

How to get the scroll position value of a document?

相关标签:
9条回答
  • 2020-12-12 21:34

    It uses HTML DOM Elements, but not jQuery selector. It can be used like:

    var height = document.body.scrollHeight;
    
    0 讨论(0)
  • 2020-12-12 21:39

    You can try this for example, this code put the scrollbar at the bottom for all DIV tags

    Remember: jQuery can accept a function instead the value as argument. "this" is the object treated by jQuery, the function returns the scrollHeight property of the current DIV "this" and do it for all DIV in the document.

    $("div").scrollTop(function(){return this.scrollHeight})
    
    0 讨论(0)
  • 2020-12-12 21:40

    Something like this should solve your problem:

    $.getDocHeight = function(){
         var D = document;
         return Math.max(Math.max(D.body.scrollHeight,    D.documentElement.scrollHeight), Math.max(D.body.offsetHeight, D.documentElement.offsetHeight), Math.max(D.body.clientHeight, D.documentElement.clientHeight));
    };
    
    alert( $.getDocHeight() );
    

    Ps: Call that function every time you need it, the alert is for testing purposes..

    0 讨论(0)
提交回复
热议问题