What is the difference between $(document).height() and $(window).height()

前端 未结 5 1529
我寻月下人不归
我寻月下人不归 2021-02-13 23:05

(Hope it is not a duplicate because I didn\'t find it when searching and googling)

I am trying to find how to detect in some fixed-height div (\'#div\') when the scroll-

5条回答
  •  长发绾君心
    2021-02-13 23:46

    In the .height() documentation:

    $(window).height();   // returns height of browser viewport
    $(document).height(); // returns height of HTML document
    

    In your case it sounds like you may want the height of the document rather than the window. Think of it this way: The window height is what you see, but the document height includes everything below or above.

    EXAMPLE

    EDIT:

    Checking for top and bottom on scroll with help from the scrollTop() method:

    var bottom = $(document).height() - $(window).height();
    
    $(document).scroll(function(){
        var position = $(this).scrollTop();
        if (position === bottom) {
            console.log("bottom");
        }else if(position === 0){
            console.log("top");   
        } else {
            console.log("scrolling");
        }
    });
    

提交回复
热议问题