如何为 iPhone 设置视口元以正确处理旋转?
所以我一直在使用:
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;"/>
让我的 HTML 内容在 iPhone 上很好地显示。 它工作得很好,直到用户 将设备旋转到横向模式,其中显示仍限制为 320 像素。
有没有一种简单的方法来指定一个视口,该视口会随着用户更改而变化 设备方向? 或者我必须求助于 Javascript 来处理这个问题?
So I've been using:
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;"/>
to get my HTML content to display nicely on the iPhone. It works great until the user
rotates the device into landscape mode, where the display remains constrained to 320px.
Is there a simple way to specify a viewport that changes in response to the user changing the
device orientation? Or must I resort to Javascript to handle that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我想出了一种稍微不同的方法,应该可以在跨平台上工作
http://www.jqui.net/tips-tricks/fixing-the-auto-scale-on-mobile-devices/
到目前为止,我已经在
Samsun Galaxy 2
iPhone 4
@media 屏幕和(最大宽度:800px){
这是移动开发的一个巨大的口号......
I have come up with a slighly different approach that should work on cross platforms
http://www.jqui.net/tips-tricks/fixing-the-auto-scale-on-mobile-devices/
So far I have tested in on
Samsun galaxy 2
iPhone 4
@media screen and (max-width:800px) {
This is a massive lip forward with mobile development ...
我自己也遇到了这个问题,我希望能够设置宽度,并在旋转时更新它并允许用户缩放和缩放页面(当前的答案提供了第一个,但阻止了后者作为副作用)..所以我想出了一个修复方法,可以使视图宽度保持正确的方向,但仍然允许缩放,尽管它不是超级简单。
首先,将以下 Javascript 添加到您正在显示的网页中:
然后在您的 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 方法中添加:
可以通过修改更多视口内容设置来完成其他调整:
http://www.htmlgoodies.com/beyond/webmaster/toolbox/article.php/3889591/Detect-and-Set-the-iPhone--iPads-Viewport-Orientation-Using-JavaScript -CSS-and-Meta-Tags.htm
另外,我知道你可以为 onresize 或类似的东西放置一个 JS 监听器来触发重新缩放,但这对我有用,因为我是从 Cocoa Touch UI 框架做的。
希望这对某人有帮助:)
I had this issue myself, and I wanted to both be able to set the width, and have it update on rotate and allow the user to scale and zoom the page (the current answer provides the first but prevents the later as a side-effect).. so I came up with a fix that keeps the view width correct for the orientation, but still allows for zooming, though it is not super straight forward.
First, add the following Javascript to the webpage you are displaying:
Then in your - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation method, add:
Additional Tweaking can be done by modifying more of the viewportal content settings:
http://www.htmlgoodies.com/beyond/webmaster/toolbox/article.php/3889591/Detect-and-Set-the-iPhone--iPads-Viewport-Orientation-Using-JavaScript-CSS-and-Meta-Tags.htm
Also, I understand you can put a JS listener for onresize or something like to trigger the rescaling, but this worked for me as I'm doing it from Cocoa Touch UI frameworks.
Hope this helps someone :)
支持所有 iPhone、所有 iPad、所有 Android。
suport all iphones, all ipads, all androids.
只是想分享,我已经尝试了响应式设计的视口设置,如果我将最大比例设置为 0.8,将初始比例设置为 1 并可缩放为 no,那么我会在纵向模式和 iPad 视图中获得最小视图对于景观:D ...这确实是一个丑陋的黑客,但它似乎有效,我不知道为什么所以我不会使用它,但有趣的结果
享受:)
just want to share, i've played around with the viewport settings for my responsive design, if i set the Max scale to 0.8, the initial scale to 1 and scalable to no then i get the smallest view in portrait mode and the iPad view for landscape :D... this is properly an ugly hack but it seems to work, i don't know why so i won't be using it, but interesting results
enjoy :)
为什么不在用户使用 javascript 旋转屏幕时重新加载页面
Why not just reload the page when the user rotates the screen with javascript
我只是想自己解决这个问题,我想出的解决方案是:
这似乎将设备锁定为 1.0 比例,无论其方向如何。 然而,它的副作用是完全禁用用户缩放(捏缩放等)。
Was just trying to work this out myself, and the solution I came up with was:
This seems to lock the device into 1.0 scale regardless of it's orientation. As a side effect, it does however completely disable user scaling (pinch zooming, etc).
对于任何仍然感兴趣的人:
http://wiki.phonegap.com/w /page/16494815/Preventing-Scrolling-on-iPhone-Phonegap-Applications
从页面:
For anybody still interested:
http://wiki.phonegap.com/w/page/16494815/Preventing-Scrolling-on-iPhone-Phonegap-Applications
From the page:
如果可以的话,您不想失去用户扩展选项。 我喜欢此处的 JS 解决方案。
You don't want to lose the user scaling option if you can help it. I like this JS solution from here.
您将其设置为无法缩放(最大缩放=初始缩放),因此当您旋转到横向模式时它无法放大。 设置最大比例 = 1.6,它将正确缩放以适应横向模式。
You're setting it to not be able to scale (maximum-scale = initial-scale), so it can't scale up when you rotate to landscape mode. Set maximum-scale=1.6 and it will scale properly to fit landscape mode.