从纵向旋转到横向时,iPad布局会向上扩展

Vic*_*scu 61 html viewport meta-tags ipad

我添加了"viewport"元标记"width=device-width,initial-scale=1.0",在iPad上,页面在横向模式下加载很好,它可以很好地切换到纵向,当我将其旋转回横向时,它会缩放页面,我必须将其缩放回到1比例.

我可以通过添加来修复此问题"maximum-scale=1.0, user-scalable=no",但我想知道是否有一种方法可以解决这个问题,而不会让用户放弃页面.

如果您有任何建议我很乐意听到,
谢谢!

sno*_*han 72

------更新------

这不再是iOS7中的问题.Scott Jehl在github scottjehl/iOS-Orientationchange-Fix上有更好的解决方案,适用于iOS6.

------原始答案------

Jeremy Keith(@adactio)在他的博客定位和规模上有一个很好的解决方案

保持标记可扩展

<meta name="viewport" content="width=device-width, initial-scale=1">
Run Code Online (Sandbox Code Playgroud)

然后,禁用了JavaScript的可扩展性,直到gesturestart这个脚本:

if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i)) {
    var viewportmeta = document.querySelector('meta[name="viewport"]');
    if (viewportmeta) {
        viewportmeta.content = 'width=device-width, minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0';
        document.body.addEventListener('gesturestart', function () {
            viewportmeta.content = 'width=device-width, minimum-scale=0.25, maximum-scale=1.6';
        }, false);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这个错误在iOS6中得到修复.查看Jeremy Keith关于此问题的最新帖子:[iOS Six Fix](http://adactio.com/journal/5802/).下面提到的[由Scott Jehl修复](https://github.com/scottjehl/iOS-Orientationchange-Fix)修改了iOS错误修复. (6认同)

And*_*her 12

Scott Jehl想出了一个出色的解决方案,它使用加速度计来预测方向的变化.此解决方案响应迅速,不会干扰缩放手势.

https://github.com/scottjehl/iOS-Orientationchange-Fix

工作原理:此修复程序通过监听设备的加速度计来预测何时将发生方向更改.当它认为方向即将发生变化时,脚本将禁用用户缩放,允许方向更改正确发生,并禁用缩放.一旦设备朝向靠近竖直方向,或者在其方向发生变化后,脚本将再次恢复缩放.这样,在页面正在使用时,永远不会禁用用户缩放.

缩小来源:

/*! A fix for the iOS orientationchange zoom bug. Script by @scottjehl, rebound by @wilto.MIT License.*/(function(m){if(!(/iPhone|iPad|iPod/.test(navigator.platform)&&navigator.userAgent.indexOf("AppleWebKit")>-1)){return}var l=m.document;if(!l.querySelector){return}var n=l.querySelector("meta[name=viewport]"),a=n&&n.getAttribute("content"),k=a+",maximum-scale=1",d=a+",maximum-scale=10",g=true,j,i,h,c;if(!n){return}function f(){n.setAttribute("content",d);g=true}function b(){n.setAttribute("content",k);g=false}function e(o){c=o.accelerationIncludingGravity;j=Math.abs(c.x);i=Math.abs(c.y);h=Math.abs(c.z);if(!m.orientation&&(j>7||((h>6&&i<8||h<8&&i>6)&&j>5))){if(g){b()}}else{if(!g){f()}}}m.addEventListener("orientationchange",f,false);m.addEventListener("devicemotion",e,false)})(this);
Run Code Online (Sandbox Code Playgroud)


小智 9

希望,这将有助于......

<head>

<style type="text/css">
<!--

/*
    I began with the goal to prevent font scaling in Landscape orientation.
    To do this, see: http://stackoverflow.com/questions/2710764/

    Later, I just wanted to magnify font-size for the iPad, leaving
    the iPhone rendering to the css code.  So ...

    (max-device-width:480px) = iphone.css
    (min-device-width:481px) and
        (max-device-width:1024px) and
            (orientation:portrait) = ipad-portrait.css
    (min-device-width:481px) and
        (max-device-width:1024px) and
            (orientation:landscape) = ipad-landscape.css
    (min-device-width:1025px) = ipad-landscape.css

*/

@media only screen and (min-device-width: 481px)
{
    html {
        -webkit-text-size-adjust: 140%;   /* none for no scaling */
    }
}

-->
</style>

</head>
Run Code Online (Sandbox Code Playgroud)

  • 嗯...这么多提出的javascript解决方案可以找到并且它仍然会跳转(当你有完全定位的元素等)或禁用缩放或类似的东西时.然后我找到了这个简单的解决方案 - 在CSS中!这使得平滑的非缩放旋转.这很简单,应该是对这个问题的所有问题的正确答案!非常感谢@John Love.这绝对是惊人的 (4认同)