Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>

<h1>Test page for ios-orientationchange-fix.js</h1>

<p>This page is a demo of a bugfix for the iOS orientation change bug, in which the page zooms beyond the viewport on orientationchange when user scaling is enabled. Summary below.</p>
Expand All @@ -30,14 +30,14 @@ <h2>Expected Results:</h2>
<h2>Actual Results (without included fix):</h2>
<p>The page is zoomed past 1.0, cropping a portion of the page from view and causing the content to be much too large.</p>

<h3>Notes: </h3>
<h3>Notes:</h3>
<p>Developers should not have to disable user-scaling to enable smooth changes in orientation.</p>


<p>Thanks!</p>
<p>Scott Jehl, Filament Group / jQuery Mobile Team</p>


<script src="ios-orientationchange-fix.js"></script>
</body>
</html>
</html>
44 changes: 22 additions & 22 deletions ios-orientationchange-fix.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,45 @@
return;
}

var doc = w.document;
var doc = w.document;

if( !doc.querySelector ){ return; }
if( !doc.querySelector ){ return; }

var meta = doc.querySelector( "meta[name=viewport]" ),
initialContent = meta && meta.getAttribute( "content" ),
disabledZoom = initialContent + ",maximum-scale=1",
enabledZoom = initialContent + ",maximum-scale=10",
enabled = true,
var meta = doc.querySelector( "meta[name=viewport]" ),
initialContent = meta && meta.getAttribute( "content" ),
disabledZoom = initialContent + ",maximum-scale=1",
enabledZoom = initialContent + ",maximum-scale=10",
enabled = true,
x, y, z, aig;

if( !meta ){ return; }
if( !meta ){ return; }

function restoreZoom(){
meta.setAttribute( "content", enabledZoom );
enabled = true;
}
function restoreZoom(){
meta.setAttribute( "content", enabledZoom );
enabled = true;
}

function disableZoom(){
meta.setAttribute( "content", disabledZoom );
enabled = false;
}
function disableZoom(){
meta.setAttribute( "content", disabledZoom );
enabled = false;
}

function checkTilt( e ){
function checkTilt( e ){
aig = e.accelerationIncludingGravity;
x = Math.abs( aig.x );
y = Math.abs( aig.y );
z = Math.abs( aig.z );

// If portrait orientation and in one of the danger zones
if( (!w.orientation || w.orientation === 180) && ( x > 7 || ( ( z > 6 && y < 8 || z < 8 && y > 6 ) && x > 5 ) ) ){
if( (!w.orientation || w.orientation === 180) && ( x > 7 || ( ( z > 6 && y < 8 || z < 8 && y > 6 ) && x > 5 ) ) ){
if( enabled ){
disableZoom();
}
}
}
}
else if( !enabled ){
restoreZoom();
}
}
}
}

w.addEventListener( "orientationchange", restoreZoom, false );
w.addEventListener( "devicemotion", checkTilt, false );
Expand Down