3

以下のコードがあります。私が達成したいのは、モバイルデバイスのスタイルを切り替えて、向きを縦向きから横向きに変更することです(iPhone4やGalaxySなどの高解像度のデバイス)

<!doctype html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />

    <title>KUBIK</title>
    <style>
        #mobile,
        #tablet { display: none; }

        @media screen and (max-width: 767px) {
            body { background-color: #FF0000; }
            #mobile { display: block; }
        }
        @media screen and (min-width: 768px) and (max-width: 1024px) {
            body { background-color: #00FFFF; }
            #tablet { display: block; }
        }       
    </style>

</head>
<body>
    <div id="mobile">MOBILE</div>
    <div id="tablet">TABLET</div>
</body>
</html>

横向きでは、iPhone 4の幅は960ピクセルなので、2番目のルールを適用する必要があります。どうすればそれを実現できますか?

4

1 に答える 1

1

iPhone には向きが切り替わる際のバグがあります。JavaScriptの修正については、この要点を参照してください

// Rewritten version
// By @mathias, @cheeaun and @jdalton

(function(doc) {

    var addEvent = 'addEventListener',
        type = 'gesturestart',
        qsa = 'querySelectorAll',
        scales = [1, 1],
        meta = qsa in doc ? doc[qsa]('meta[name=viewport]') : [];

    function fix() {
        meta.content = 'width=device-width,minimum-scale=' + scales[0] + ',maximum-scale=' + scales[1];
        doc.removeEventListener(type, fix, true);
    }

    if ((meta = meta[meta.length - 1]) && addEvent in doc) {
        fix();
        scales = [.25, 1.6];
        doc[addEvent](type, fix, true);
    }

}(document));
于 2012-01-28T15:05:27.090 に答える