screen.width 和 screen.height 在不同的 API/设备中具有不同的值

发布于 2025-01-06 12:23:32 字数 1027 浏览 6 评论 0原文

编辑:也发生在 $('body').width() 和 window.outerWidth

API 2.3.3 HVGA

旋转设备输出相同屏幕宽度(320)之前和之后

API 3.0 WXGA

宽度和高度切换每次旋转例如

从 screenWidth 开始:1280 屏幕高度:800 我旋转90度 现在 screenWidth:800 screenheight: 1280

那么如果我想对旋转进行某些更改我该怎么办 根据维度并想要定位所有 API?我需要一个对所有设备都相同的值。

编辑:对于某些事情,我需要像素值,而不是百分比。这就是为什么我使用 Javascript 根据屏幕宽度计算尺寸。这是可行的,因为屏幕宽度也是像素值,并且我可以保持比例。但如果有时 screen.width 给我当前宽度,而其他人则不给我,我就无法使用它......

->问题是我开始使用一个滑块,它使用绝对布局并且需要所有内容的像素值。我不想重新实现滑块,因此我决定使用屏幕宽度动态计算图像的大小和整个布局。并使用这些值初始化滑块。

更新

看这里是与我想做的类似的方法:

http://ryangillespie.com/phonegap.php#/phonegap.php

2011 年 6 月 18 日入职 “一屏分辨率统治所有”

我也尝试过这个例子,将其复制粘贴到我的代码中。但它也不起作用。 window.outerWidth 与我描述的 screen.width (以及 JQuery $('body').width())具有相同的问题。只要设备不旋转它就可以工作 - 它初始化得很好。但在第一次旋转时,根据设备的不同,我遇到了问题。在某些情况下,它按预期工作,在另一些情况下,它交换值,以便我在纵向模式下获得较大的宽度,在横向模式下获得较短的宽度,在其他情况下,它始终给出固定的宽度和高度,在其他情况下,它根本不旋转。 ..

Edit: also happens with $('body').width() and window.outerWidth

API 2.3.3 HVGA

Before and after rotating device outputs same screen width (320)

API 3.0 WXGA

Width and height toggle each rotation for example

starts with screenWidth:1280 screenheight: 800
I rotate 90
now has screenWidth:800 screenheight: 1280

so what do I do if I want to make certain changes on rotations
according to dimensions and want to target all APIs? I need a value which is the same for all devices.

Edit: For certain things I need pixel values, not percentages. That's why I'm using Javascript to calculate size based on screen width. This would work, since screen width is also pixel values and I can keep things proportional. But if sometimes screen.width gives me the current width, and others not, I can't use it...

-> The thing is I started working with a slider which uses absolute layout and needs pixel values for everything. I don't want to reimplement the slider, so I decided to calculate dynamically the size of the images and the whole layout using screen width. And initialize the slider with these values.

update

Look here is a similar approach to what I'm trying to do:

http://ryangillespie.com/phonegap.php#/phonegap.php?

Entry of June 18, 2011
"One Screen Resolution to Rule Them All"

I tried also with exactly that example, copy pasting it in my code. But it doesn't work either. window.outerWidth has the same problems as I'm describing for screen.width (as well as JQuery $('body').width()). It works as long as the device isn't rotated - it initializes well. But at the first rotation, depending of the device, I get problems. In some it works as expected, in others it interchanges the values, so that I get large width in portrait mode and short in landscape, in others it gives fixed width and height all time, in others it doesn't rotate at all....

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

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

发布评论

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

评论(4

凉月流沐 2025-01-13 12:23:33

响应式网页设计技术。我在博客上给出了一个非常简短的示例以及书籍推荐。

http://simonmacdonald.blogspot.com/2012 /01/on-8-day-of-phonegapping-multiple.html

Responsive web design techniques. I give a super brief example on my blog along with a book recommendation.

http://simonmacdonald.blogspot.com/2012/01/on-eight-day-of-phonegapping-multiple.html

迷你仙 2025-01-13 12:23:33

我在两个 PhoneGap 应用程序中使用媒体查询。没有 JavaScript,除了
出现异常的情况。

例如,“base”css 可以适用于宽度 320 和纵向,
然后使用CSS的级联效果:-)添加块,例如:

@media screen and (max-width: 480px) and (orientation:portrait) { make stuff更大}

@media all and (min-width: 800px) { make stuff更大}

在我的链接CSS文件(以及device/os/phonegap
处理方向变化)新的布局会神奇地自动发生。

注意:我通过阅读西蒙的博客和他建议的材料了解到了这一切。

I use media queries in two of my PhoneGap Apps. No javascript, except in
the case of anomalies.

For example, the "base" css could be for width 320 and portrait,
then using the cascading effect of css :-) add blocks like:

@media screen and (max-width: 480px) and (orientation:portrait) { make stuff bigger}

@media all and (min-width: 800px) { make stuff even bigger }

With queries like these in my link'd css files (and the device/os/phonegap
handling of orientation changes) the new layouts happen auto-magically.

NOTE: I learned all this from reading Simon's blog and the materials he suggested.

夜唯美灬不弃 2025-01-13 12:23:33

巧合的是,我发现这是有效的:

$(window).resize(function() {
    updateScaling($('body').width());
}); 

它总是被调用并传递正确的宽度。据我记得它也适用于 screen.width

在 updateScaling 中我计算一个scalingFactor并调整我的元素。

我尝试了响应式 CSS、媒体查询等,但在某些时候它不再有意义,因为我无论如何都要根据当前幻灯片和新宽度以及其他需要脚本的内容重新计算滑块 UL 的边距。所以我用脚本做了一切。

我删除了 window.onorientationchange。

Coincidentally I found that this works:

$(window).resize(function() {
    updateScaling($('body').width());
}); 

This is always called and passes correct width. As far as I remember it also works with screen.width

In updateScaling I calculate a scalingFactor and adjust my elements.

I tried out responsive CSS, media queries and so on, but at some point it didn't make sense anymore, because I have anyways to recalculate the margin of slider's UL based on current slide and new width - and other stuff which needs script. So I made everything with script.

I removed window.onorientationchange.

沫雨熙 2025-01-13 12:23:33

我不知道phonegap如何为您呈现信息,但对于本机Android应用程序,您通常会为各种显示密度和屏幕尺寸/宽度声明不同的布局和图像资源。有关详细信息,请参阅此页面:http://developer.android.com/guide/practices/ screen_support.html

I'm not aware how phonegap presents in information for you, but for a native Android application you typically declare different layouts and image resources for various display densities and screen sizes/widths. See this page for more information: http://developer.android.com/guide/practices/screens_support.html

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