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
Difference between screen and window property?
提问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 Screen
object, it's actually window.screen
.
没有Screen
对象,它实际上是window.screen
。
A window
object (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 screen
object refers to the actual monitor window or desktop size. Note that if you have a multi-mon setup then you will have multiple screen
objects. A window
object belongs to a single screen
, though not very window
belongs to the same screen
. I do not know what happens when a browser window spans multiple screen
s.
该screen
目标是指实际监视器窗口或桌面大小。请注意,如果您有多个 mon 设置,那么您将拥有多个screen
对象。一个window
对象属于一个单一的screen
,虽然不是很window
属于同一个screen
。我不知道当浏览器窗口跨越多个screen
s时会发生什么。
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.screen和https://developer.mozilla.org/en-US/docs/DOM/window
回答by piyush
screen.height
gives you height of your monitor screen.screen.width
gives you width of your monitor screen.
screen.height
为您提供显示器屏幕的高度。screen.width
为您提供显示器屏幕的宽度。
window.height
gives you height of your browser window.window.width
gives 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 中的窗口、屏幕和文档有什么区别?几乎是同样的问题。解释接受的答案并添加一些我认为可以使用的信息:
window
in the root object. Any variables or functions you define are in some way children of the window
object. So if you do var something="blah"
in a script tag, you can later access that variable in 3 ways - something
, window.something
or window["something"]
.
window
在根对象中。您定义的任何变量或函数在某种程度上都是window
对象的子代。所以,如果你var something="blah"
在一个脚本标签,您可以稍后访问3种方式变量- something
,window.something
或window["something"]
。
screen
is one of the children of window that is created by the browser. however, for the same reason that you can access window.something
as something
, you can access it either as window.screen
or 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 这样的框架,在这种情况下,他们可能会为您提供此信息而无需担心浏览器兼容性)。