【问题标题】:What is the difference between $(document).height() and $(window).height()$(document).height() 和 $(window).height() 有什么区别
【发布时间】:2013-01-08 09:03:22
【问题描述】:

(希望它不是重复的,因为我在搜索和谷歌搜索时没有找到它)

当滚动条到达后一个 div 的底部时,我试图找到如何在某些固定高度的 div ('#div') 中进行检测。我应该使用$(document).height()$(window).height() 来检测此事件吗?

编辑:我的 div 是固定高度的,我设置了自动滚动,那么如何处理呢?如果我想使用 $('#div').height(),这个高度是固定的....

【问题讨论】:

  • document 的高度由内容和 CSS 动态设置,而window 的高度由用户(浏览器窗口)设置。
  • 你可以将“滚动”事件绑定到你想观察的div上
  • 你试过了吗?它们返回不同的值

标签: javascript jquery scroll window


【解决方案1】:

.height() 文档中:

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

在您的情况下,听起来您可能想要document 的高度而不是window。可以这样想:window 高度就是您所看到的,但document 高度包括下方或上方的所有内容。

EXAMPLE

编辑

scrollTop() 方法的帮助下检查滚动的顶部和底部:

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");
    }
});

【讨论】:

  • 是的,我已经编辑了我的帖子,因为我的 div 设置为自动滚动
【解决方案2】:

文档高度是整个文档的整个高度,即使是可视区域之外的高度。如果页面很长,这可能是数千像素。窗口高度只是可视区域。

【讨论】:

  • 是的,我明白,但我已经编辑了我的帖子,因为我的 div 设置为自动滚动
  • 所以你有一个固定高度的 div,并且内容比那个高度长,所以它正在创建一个滚动条,你试图获取这个内部内容的高度,对吗?如果是这种情况,我相信您想使用 scrollHeight developer.mozilla.org/en-US/docs/DOM/element.scrollHeight
  • 在该链接中还有一个我认为您正在尝试完成的示例:确定元素是否已完全滚动如果元素位于其滚动的末尾,则以下等效返回 true,如果不是,则为假。 element.scrollHeight - element.scrollTop === element.clientHeight
  • 好吧,事实上我发现它就在我身边,我知道 scrollHeight 但不知道 scrollTop :)
【解决方案3】:

$(window).height() 和 $(document).height() 函数的区别。

$(window).height() 函数:

理想情况下 $(window).height() 返回浏览器窗口的像素减去高度。这始终是当前浏览器窗口的高度。如果你调整浏览器的大小,这个值应该会改变。

$(document).height() 函数: $(document).height() 返回正在渲染的文档高度的无单位像素值。

在 HTML 中,如果你不声明 DOCTYPE,那么所有时间 HTML 页面都会返回 $(window).height() 和 $(document).height() 相同的值。

<html>
    <head>
        <script type='text/javascript' src='http://code.jquery.com/jquery-1.10.1.js'></script>


        <script type='text/javascript'>

        $(document).ready(function(){
            $('#winheight').text($(window).height());
            $('#docheight').text($(document).height());
        });

        </script>
    </head>
    <body>
        <div id="console">
            $(window).height() = <span id="winheight"></span> <br/>
            $(document).height() = <span id="docheight"></span>
        </div>
        <p>Lorem ipsum dolor sit amet</p>
    </body>
</html>

输出:

$(window).height() = 750 
$(document).height() = 750
Lorem ipsum dolor sit amet

声明 DOCTYPE 后返回完美值。

<!DOCTYPE HTML>
<html>
    write above code here
</html>

输出:

$(window).height() = 750 
$(document).height() = 750
Lorem ipsum dolor sit amet

【讨论】:

    【解决方案4】:

    文档的高度不一定与窗口的高度相同。如果您有一个只有一个 DIV 和一点点文本的简单文档,则文档高度与窗口高度相比将是微不足道的。

    【讨论】:

      【解决方案5】:

      这是来自 jQuery 源代码:

      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]);
      }
      

      所以$(window) 使用clientHeight。其中,正如@Drew 正确提到的可见屏幕区域的高度。

      对于$(document),将使用当前页面的整个滚动高度。

      【讨论】:

      • 那么如何处理我的 div 是固定高度并且我设置了自动滚动?
      • 嗯?您尝试做的事情与$(window)$(document) 高度无关。您只需要 scrollTopscrollHeight 用于该元素(您正在谈论的 div)。
      猜你喜欢
      • 1970-01-01
      • 2013-03-19
      • 2013-04-22
      • 1970-01-01
      • 1970-01-01
      • 2014-07-18
      • 1970-01-01
      • 1970-01-01
      • 2012-01-29
      相关资源
      最近更新 更多