javascript window.screen.width/height 不应该对应于实际的屏幕宽度/高度吗?

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

Shouldn't window.screen.width/height correspond to actual screen width/height?

javascriptwebkitwindowdimensionskindle

提问by Ates Goral

Toying around with the WebKit browser in the new Kindle 3G, I noticed that window.screen.widthand window.screen.heightdon't reflect the actual screen dimensions. The physical screen (or rather, paper) dimensions are 800 x 600. I get:

在新 Kindle 3G 中玩弄 WebKit 浏览器时,我注意到了这一点window.screen.widthwindow.screen.height并没有反映实际的屏幕尺寸。物理屏幕(或者更确切地说,纸)尺寸是800 x 600。我得到:

  • 800 x 506in landscape mode
  • 600 x 706in portrait mode
  • 800 x 506横向模式
  • 纵向模式下600 x 706

But interestingly, both Chrome and Safari (which are also WebKit) running on my desktop report the actual screen resolution.

但有趣的是,在我的桌面上运行的 Chrome 和 Safari(也是 WebKit)都报告了实际的屏幕分辨率。

According to MDC, these properties are not part of any specification, so there's probably no strict definition of what width/height should report. But, shouldn't it be expected that they reflect the actual screen dimensions?

根据MDC,这些属性不是任何规范的一部分,因此可能没有严格定义应报告的宽度/高度。但是,难道不应该期望它们反映实际的屏幕尺寸吗?

回答by HotFudgeSunday

Update:

更新:

The issue we see in the emulator with window.screen.width is when we use screens which don't reflect the actual pixels of the device. So what you see on the screen is 320 and what the device has is 480 or whatever. I don't understand, though, why the value of screen width would give the emulator size on the screen and not the actual pixels.

我们在带有 window.screen.width 的模拟器中看到的问题是当我们使用不反映设备实际像素的屏幕时。所以你在屏幕上看到的是 320 而设备有的是 480 或其他什么。但是,我不明白为什么屏幕宽度的值会给出屏幕上的模拟器大小而不是实际像素。

This thing might be the same issue with the devices? If their density is higher at some sizes... for whatever the reason this could translate to some wrong screen width size?

这东西可能是与设备相同的问题?如果它们的密度在某些尺寸下更高......无论出于何种原因,这可能会转化为某些错误的屏幕宽度尺寸?

Anyway, read below for my solution.

无论如何,请阅读下面的解决方案。



screen.availWidth does not work for me on certain screen sizes on the emulator.

screen.availWidth 在模拟器上的某些屏幕尺寸上对我不起作用。

Only thing is working for me now is:

现在唯一对我有用的是:

window.innerWidth
window.innerHeight

Which will return the value of the Viewport. In my case I'm running an HTML5 app. This values will not update on zoom, apparently.

这将返回视口的值。就我而言,我正在运行一个 HTML5 应用程序。显然,此值不会在缩放时更新。

They have some issues with this sizes on Android's Webkit. You can see the devs from Android talking about it here. Probably fixed in Honeycomb.

他们在 Android 的 Webkit 上对这个尺寸有一些问题。您可以在此处看到来自 Android 的开发人员在谈论它。可能固定在 Honeycomb 中。

Somebody claims it takes some sizes as if the soft keyboard would be present.

有人声称它需要一些尺寸,就好像软键盘会存在一样。

回答by Harmon Wood

It would seem that screen.availWidth and screen.width are both returning the availWidth all the time, on many mobile operating systems.

在许多移动操作系统上,screen.availWidth 和 screen.width 似乎一直都在返回availWidth。

I was unable to find any documentation on this however I did tested Android 2.2 and got the screen size minus the top menu bar exactly no matter what a requested.

我无法找到有关此的任何文档,但是我确实测试了 Android 2.2,并且无论请求什么,屏幕尺寸减去顶部菜单栏。

回答by pepkin88

I think WebKit can do nothing if it's getting wrong values from system. Sure these values should be represented by screen.availWidth and screen.availHeight, but I think this is related to event.screenX and event.screenY for mouse (pointer) positions.

我认为如果 WebKit 从系统获取错误的值,它就无能为力。当然这些值应该由 screen.availWidth 和 screen.availHeight 表示,但我认为这与鼠标(指针)位置的 event.screenX 和 event.screenY 相关。

回答by carlosveucv

The difference are because the size of the Android status bar and or the bottom bar.

区别在于 Android 状态栏和/或底部栏的大小。