jQuery $(window).height() vs $(document).height

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14035819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:12:22  来源:igfitidea点击:

$(window).height() vs $(document).height

jquery

提问by Pawan Nogariya

I am having problem of getting wrong height with

我有错误高度的问题

$(window).height();

and got the similar question here

并在这里得到了类似的问题

In my case when I try

在我的情况下,当我尝试

$(document).height();

it seems to return me correct result

它似乎返回了正确的结果

window height returns 320

窗口高度返回 320

while document height returns 3552!

而文档高度返回 3552!

I found thisquestion too

我也发现了这个问题

But in my case window already gets loaded completely as I am calling height function after few ajax operations

但在我的情况下,窗口已经完全加载,因为我在几次 ajax 操作后调用高度函数

So what is the best way to know the height of the current window?

那么知道当前窗口高度的最佳方法是什么?

Edit:

编辑:

enter image description hereenter image description here

enter image description hereenter image description here

回答by Sayan

Well you seem to have mistaken them both for what they do.

好吧,你似乎把他们都误认为是他们所做的。

$(window).height()gets you an unit-less pixel value of the height of the (browser) windowaka viewport. With respect to the web browsers the viewport here is visible portion of the canvas(which often is smaller than the document being rendered).

$(window).height()为您提供(浏览器)窗口又名视口高度的无单位像素值。对于 Web 浏览器,这里的视口是画布的可见部分(通常小于正在呈现的文档)。

$(document).height()returns an unit-less pixel value of the height of the documentbeing rendered. However, if the actual document's body height is less than the viewport height then it will return the viewport height instead.

$(document).height()返回正在渲染的文档高度的无单位像素值。但是,如果实际文档的正文高度小于视口高度,则它将返回视口高度。

Hope that clears things a little.

希望能把事情弄清楚一点。

回答by Ashraf Bin Akbar

This fixed me

这固定了我

var width = window.innerWidth;
var height = window.innerHeight;

回答by gamehelp16

AFAIK $(window).height();returns the height of your window and $(document).height();returns the height of your document

AFAIK$(window).height();返回窗口$(document).height();的高度并返回文档的高度

回答by zsytssk

you need know what it is mean about document and window.

您需要知道文档和窗口的含义。

  1. The window object represents an open window in a browser.click here
  2. The Document object is the root of a document tree.click here
  1. window 对象代表浏览器中打开的窗口。点击这里
  2. Document 对象是文档树的根。点击这里

回答by YziaFromIran

$(document).height:if your device heightwas bigger. Your page has Not any scroll;

$(document).height:如果您的设备height更大。您的页面没有任何滚动;

$(document).height:assume you have not scroll and return this height;

$(document).height:假设你没有滚动并返回这个height

$(window).height:return your page heighton your device.

$(window).height:height在您的设备上返回您的页面。