javascript 屏幕和窗口属性之间的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16162639/
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-10-27 03:31:13  来源:igfitidea点击:

Difference between screen and window property?

javascriptasp.netweb-applications

提问by Elango_Thanumalayan

In my Web application , am in need to use the browser window's Height & Width. So I used Screen.Width , Screen.Heightproperties in JavaScript to get the Width & Height. While am surfing I got another property as Window.Width , Window.Height. Can anyone tell me , which property gives me the Size of Browser window.....Screen (or) Window ?

在我的 Web 应用程序中,我需要使用浏览器窗口的高度和宽度。所以我在 JavaScript 中使用Screen.Width 、 Screen.Height属性来获取宽度和高度。在冲浪时,我得到了另一个属性Window.Width , Window.Height。谁能告诉我,哪个属性给了我浏览器窗口的大小.....屏幕(或)窗口?

回答by Dai

There is no Screenobject, it's actually window.screen.

没有Screen对象,它实际上是window.screen

A windowobject (obtained through document.defaultView) returns information about both the window and the viewport. To get the application window size use window.outerHeight, to get viewport size use window.innerHeight.

window对象(通过获得document.defaultView)返回有关窗口和视口双方的信息。获取应用程序窗口大小使用window.outerHeight,获取视口大小使用window.innerHeight

The screenobject refers to the actual monitor window or desktop size. Note that if you have a multi-mon setup then you will have multiple screenobjects. A windowobject belongs to a single screen, though not very windowbelongs to the same screen. I do not know what happens when a browser window spans multiple screens.

screen目标是指实际监视器窗口或桌面大小。请注意,如果您有多个 mon 设置,那么您将拥有多个screen对象。一个window对象属于一个单一的screen,虽然不是很window属于同一个screen。我不知道当浏览器窗口跨越多个screens时会发生什么。

From all this, you can determine that if you are running a full-screen browser then window.outerHeight == window.innerHeight == screen.height.

综上所述,您可以确定,如果您正在运行全屏浏览器,那么window.outerHeight == window.innerHeight == screen.height.

Source: https://developer.mozilla.org/en-US/docs/DOM/window.screenand https://developer.mozilla.org/en-US/docs/DOM/window

来源:https: //developer.mozilla.org/en-US/docs/DOM/window.screenhttps://developer.mozilla.org/en-US/docs/DOM/window

回答by piyush

screen.heightgives you height of your monitor screen.
screen.widthgives you width of your monitor screen.

screen.height为您提供显示器屏幕的高度。
screen.width为您提供显示器屏幕的宽度。

window.heightgives you height of your browser window.
window.widthgives you width of your browser window.

window.height为您提供浏览器窗口的高度。
window.width为您提供浏览器窗口的宽度。

回答by Chris O'Kelly

What is the difference between window, screen, and document in Javascript?is pretty much this same question. To paraphrase the accepted answer and add some info that I feel it could use:

Javascript 中的窗口、屏幕和文档有什么区别?几乎是同样的问题。解释接受的答案并添加一些我认为可以使用的信息:

windowin the root object. Any variables or functions you define are in some way children of the windowobject. So if you do var something="blah"in a script tag, you can later access that variable in 3 ways - something, window.somethingor window["something"].

window在根对象中。您定义的任何变量或函数在某种程度上都是window对象的子代。所以,如果你var something="blah"在一个脚本标签,您可以稍后访问3种方式变量- somethingwindow.somethingwindow["something"]

screenis one of the children of window that is created by the browser. however, for the same reason that you can access window.somethingas something, you can access it either as window.screenor screen. This contains the properties of the actual screen, and it is where I would go to get the details you want (unless you have access to a framework like jQuery or Prototype, in which case they can probably give you this information without worries about browser compatibility).

screen是浏览器创建的窗口的子窗口之一。但是,出于与您可以window.something作为访问相同的原因,您可以something作为window.screen或访问它screen。这包含实际屏幕的属性,我将在此处获取您想要的详细信息(除非您可以访问像 jQuery 或 Prototype 这样的框架,在这种情况下,他们可能会为您提供此信息而无需担心浏览器兼容性)。