【问题标题】:How do I decide the height and scrolling position of window in jQuery?如何在 jQuery 中确定窗口的高度和滚动位置?
【发布时间】:2015-08-24 00:42:36
【问题描述】:

我需要在 jQuery 中获取窗口的高度和滚动偏移量,但我没有在 jQuery 文档或 Google 中找到它。我 90% 确定有一种方法可以访问元素的高度和滚动顶部(大概包括窗口),但我找不到具体的。我被困在下面的代码中

var height = $(window).height();
var scrollTop =$(window).scrollTop();

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    我不确定 jQuery Approved™ 的执行方式是什么,但使用纯 JavaScript:

    var height = window.innerHeight;
    var scrollTop = window.pageYOffset;
    

    【讨论】:

      【解决方案2】:

      您必须了解 $(window) 和 $(document) 之间的基本区别。

      $(window) 指的是视口

      $(document)指的是实际的html文档

      因此,在您的情况下,您将使用 $(document).scrollTop(); 作为滚动距离,并根据需要(实际视口高度或文档高度)$(document).height()$(window).height()

      编辑:查看http://api.jquery.com/height/http://api.jquery.com/scrolltop/

      【讨论】:

        猜你喜欢
        • 2010-09-23
        • 1970-01-01
        • 2021-01-12
        • 1970-01-01
        • 1970-01-01
        • 2013-05-12
        • 1970-01-01
        • 1970-01-01
        • 2015-04-27
        相关资源
        最近更新 更多