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

前端 未结 5 1005
不思量自难忘°
不思量自难忘° 2021-02-13 23:25

(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:39

    Here's the code from jQuery source:

    if (jQuery.isWindow(elem)) {
        // As of 5/8/2012 this will yield incorrect results for Mobile Safari, but there
        // isn't a whole lot we can do. See pull request at this URL for discussion:
        // https://github.com/jquery/jquery/pull/764
        return elem.document.documentElement["client" + name];
    }
    
    // Get document width or height
    if (elem.nodeType === 9) {
        doc = elem.documentElement;
    
        // Either scroll[Width/Height] or offset[Width/Height] or client[Width/Height], whichever is greatest
        // unfortunately, this causes bug #3838 in IE6/8 only, but there is currently no good, small way to fix it.
        return Math.max(
        elem.body["scroll" + name], doc["scroll" + name],
        elem.body["offset" + name], doc["offset" + name],
        doc["client" + name]);
    }
    

    So for $(window) clientHeight is used. Which, as @Drew correctly mentioned the height of visible screen area.

    For $(document) the whole scroll height of the current page will be used.

提交回复
热议问题