JS“窗口”宽度-高度与“屏幕”宽高?

发布于 2024-11-14 12:35:40 字数 351 浏览 4 评论 0原文

当我查看这段代码时,我有点想知道:

// Get the screen height and width
var maskHeight = $(document).height();
var maskWidth = $(window).width();

...

// Get the window height and width
var winH = $(window).height();
var winW = $(window).width();

$(document).height();$(window).height(); 之间有什么区别?

I am wondering a little when I look at this code:

// Get the screen height and width
var maskHeight = $(document).height();
var maskWidth = $(window).width();

...

// Get the window height and width
var winH = $(window).height();
var winW = $(window).width();

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

秋凉 2024-11-21 12:35:40

窗口是顶级客户端对象,其中包含文档。此 jsFiddle 显示 $(window).height()$(document).height() 返回相同的值: http://jsfiddle.net/jackrugile/5xSuv/

窗口是视口的大小,不包括任何 chrome 或浏览器界面,如果我没记错的话。我相信两者的值将始终相同,除非您引用窗口内的 iframe 之类的内容。

Window is the top level client side object, which contains the document. This jsFiddle shows that both $(window).height() and $(document).height() return the same value: http://jsfiddle.net/jackrugile/5xSuv/

Window is the size of the viewport and does not include any of the chrome or browser interface, if I am not mistaken. I believe that the values of both will always be the same, unless you are referencing something like an iframe within a window.

℡寂寞咖啡 2024-11-21 12:35:40

$(document).height 是视口的内部区域,基本上是从工具栏/URL 栏的底部到状态栏/底部滚动条/窗口底部。 $(window).height 获取窗口的整个高度,包括地址栏和滚动条等内容。

$(document).height is the inside area of the viewport, essentially from the bottom of your toolbar/url bar to your status bar/bottom scroll bar/bottom of the window. The $(window).height gets the entire height of the window, including things like the address bar and scroll bars.

骄傲 2024-11-21 12:35:40

@jackrugile 的 jsfiddle 代码为文档和窗口返回相同的值,因为 jsfiddle 代码在 iframe 内运行。

为了让事情更清楚,如果不运行 Iframe -

  • $(window).height() 将返回视口区域的高度,不包括页面上存在的任何工具栏的高度。可以通过打开 firebug 控制台(如果是 firefox)或 google chrome 控制台按 F12 键并触发 $(window).height() 来进行同样的实验,如果从浏览器中添加/删除任何工具栏或简单地更改firebug 或 chrome 调试器的高度。

    它将始终返回浏览器窗口的高度减去所有工具栏的高度。

  • $(document).height() 将返回页面内容的高度,页面的长度。
    工具栏或浏览器窗口的高度(无论是否调整大小)不会影响其值。
    在发布我的答案之前,chrome 中的值约为 2407,firefox 中的值约为 2410。

希望它能有所帮助并使事情变得更加清晰。

The jsfiddle code by @jackrugile returns the same values for document and window because the jsfiddle code is running inside an iframe.

To make things more clear if not running Iframes -

  • $(window).height() will return the height of the viewport area excluding the height of any of the toolbars present on the page. The same can be experimented here by opening a firebug console (if firefox) or google chrome console pressing F12 key and firing $(window).height() which will always vary if add / remove any of the toolbars from browser or simply change the height of the firebug or chrome debugger.

    It will always return the height of your browser window substracting the height of all toolbars.

  • $(document).height() will return the height of your page contents, how long your page is.
    The height of toolbars or browser window (if re-sized or not) doesn't affects it's value.
    Before posting my answer it was around 2407 in chrome and 2410 in firefox.

Hope it helps and make things more clear.

瞎闹 2024-11-21 12:35:40

屏幕 -- 您的屏幕:尺寸值随显示器尺寸而变化。

screen.availWidth  //There is no screen.height 

窗口或文档——浏览器的窗口(浏览器视口,不包括工具栏和滚动条)。如果页面可滚动,则忽略不可见的可滚动部分。 IE9及以上版本使用“window”,很简单。

window.innerHeight    //IE9, Chrome, Safari, Firefox
document.documentElement(or body).clientHeight    //IE 8,7,6,5

注意:窗口和文档并不相同。文档对象(来自 DOM)是 Window 对象(来自 BOM)的属性。但给出相同的尺寸。
从 W3Schools 来看,我认为“Window”是新浏览器版本(IE9、Chrome、Firefox 等)的表示法,“document”是 IE 8、7、6、5 的表示法。

http://www.w3schools.com/js/js_window.asp,并且还经过测试上面是在不同尺寸的显示器上使用一个简单的 aspx 页面。

Screen -- Your screen: size value changes with your monitor size.

screen.availWidth  //There is no screen.height 

Window or Document -- The Browser's window (the browser viewport, NOT including toolbars and scrollbars). Ignores the invisible scrollable part if the page is scrollable. Use 'window' for IE9 and above, its simple.

window.innerHeight    //IE9, Chrome, Safari, Firefox
document.documentElement(or body).clientHeight    //IE 8,7,6,5

Note: Window and Document are not the same. Document object (from DOM) is a property of the Window object (from BOM). But give out the same size.
From W3Schools, I would like to think that 'Window' is the notation for the new browser versions (IE9, Chrome, Firefox etc) and 'document' is for IE 8,7,6,5.

http://www.w3schools.com/js/js_window.asp, and also tested the above with a simple aspx page on differently sized monitors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文