19

我需要页面的视口宽度为横向 950 像素,纵向为 630 像素。不幸的是,这些像素宽度是不灵活的。

我正在使用 CSS 媒体查询根据方向调整 DOM 内容的宽度。

我正在侦听 JS 中的方向更改事件,以在方向更改时调整视口的大小。

初始页面加载(纵向或横向)时一切看起来都不错。

但是,在方向更改后,Mobile Safari 会保留以前的比例设置,而不是使视口适合屏幕。双击或两次将其理顺。但我需要它是自动的。

我附上了下面的源代码,并将其上传到演示URL

以下是方向更改后的屏幕截图:从横向到纵向,从纵向到横向。

如何强制 Safari 自动将视口宽度设置为屏幕宽度?还是我对这一切都错了?

更改视口中的各种比例设置没有帮助。设置maximum-scale=1.0orinitial-scale=1.0似乎会覆盖我的width, 设置widthdevice-width(即我的 iPad 2 上的 1024 或 768),留下死边距空间。设置minimum-scale=1.0似乎什么都不做。

从横向到纵向

从纵向到横向

<!DOCTYPE html>
<html>
    <head>
      <title>iPad orientation</title>

      <meta name="viewport" content="user-scalable=yes">

      <script src="jquery-1.4.2.min.js" type="text/javascript"></script>

      <script type="text/javascript" charset="utf-8">;
        /**
        * update viewport width on orientation change
        */
        function adapt_to_orientation() {
          // determine new screen_width
          var screen_width;
          if (window.orientation == 0 || window.orientation == 180) {
            // portrait
            screen_width = 630;
          } else if (window.orientation == 90 || window.orientation == -90) {
            // landscape
            screen_width = 950;
          }

          // resize meta viewport
          $('meta[name=viewport]').attr('content', 'width='+screen_width);
        }

        $(document).ready(function() {

          // bind to handler
          $('body').bind('orientationchange', adapt_to_orientation);

          // call now
          adapt_to_orientation();
        });

      </script>
      <style type="text/css" media="screen">
        body {
          margin: 0;
        }
        #block {
          background-color: #333;
          color: #fff;
          font-size: 128px;

          /* landscape */
          width: 950px;
        }
        #block .right {
          float: right
        }
        @media only screen and (orientation:portrait) {
          #block {
            width: 630px;
          }
        }
      </style>
    </head>
    <body>
      <div id="block">
        <div class="right">&rarr;</div>
        <div class="left">&larr;</div>
      </div>
    </body>
</html>
4

4 回答 4

10

我遇到了这个问题并编写了一些 JavaScript 来解决它。我把它放在 Github 上:

https://github.com/incompl/ios-reorient

为了您的方便,这里是代码:

window.document.addEventListener('orientationchange', function() {
  var iOS = navigator.userAgent.match(/(iPad|iPhone|iPod)/g);
  var viewportmeta = document.querySelector('meta[name="viewport"]');
  if (iOS && viewportmeta) {
    if (viewportmeta.content.match(/width=device-width/)) {
      viewportmeta.content = viewportmeta.content.replace(/width=[^,]+/, 'width=1');
    }
    viewportmeta.content = viewportmeta.content.replace(/width=[^,]+/, 'width=' + window.innerWidth);
  }
  // If you want to hide the address bar on orientation change, uncomment the next line
  // window.scrollTo(0, 0);
}, false);
于 2013-08-15T19:41:01.130 回答
8

*-scale好吧,答案毕竟在于属性。

maximum-scale您可以通过将和设置minimum-scale为相同的值来强制使用特定的比例。但是您必须通过将元标记设置为<meta name='viewport' content='user-scalable=no' />.

请注意,这些*-scale设置是相对于设备的屏幕尺寸而言的——因设备而异。screen幸运的是,您可以在 JS中的对象中查找这些内容。

所以,如果你的内容宽度低于 980,你的强制比例应该是screen_dimension / content_width.

我上面的 JS 的这个更新版本使方向更改工作顺利。在 iPhone 4 和 iPad 2 上测试。

    function adapt_to_orientation() {
      var content_width, screen_dimension;

      if (window.orientation == 0 || window.orientation == 180) {
        // portrait
        content_width = 630;
        screen_dimension = screen.width * 0.98; // fudge factor was necessary in my case
      } else if (window.orientation == 90 || window.orientation == -90) {
        // landscape
        content_width = 950;
        screen_dimension = screen.height;
      }

      var viewport_scale = screen_dimension / content_width;

      // resize viewport
      $('meta[name=viewport]').attr('content',
        'width=' + content_width + ',' +
        'minimum-scale=' + viewport_scale + ', maximum-scale=' + viewport_scale);
    }
于 2012-08-24T17:56:15.163 回答
3

使用这篇文章的智慧,只需获取脚本容器的宽度和高度(窗口宽度和高度)...... https://stackoverflow.com/a/9049670/818732

使用这个视口:target-densitydpi=device-dpi, width=device-width, initial-scale=1, user-scalable=no解决了我在 android 和 ios 上的问题。请注意,target-densitydpi 将在除 Android 之外的所有其他设备上被标记,但不会造成任何伤害。它将确保没有自动缩放。

于 2013-07-19T15:54:38.233 回答
1

在 iOS 上遇到了类似的问题,经过一些测试后你可以在这里找到解决方案

视口宽度更改后在 iOS 上重置缩放

在我的解决方案中,缩放被禁用,但您可以自由删除“user-scalable=no”以使其可缩放。

于 2015-12-23T14:18:38.787 回答