Skip to content

Instantly share code, notes, and snippets.

@scottjehl
Created January 6, 2012 00:27
Show Gist options
  • Save scottjehl/1568180 to your computer and use it in GitHub Desktop.
Save scottjehl/1568180 to your computer and use it in GitHub Desktop.
Fix iOS Orientation Change Zoom Bug
/*
NOTE!!!!
The most updated version of this code is here:
https://github.com/scottjehl/iOS-Orientationchange-Fix
A fix for the dreaded iOS orientationchange zoom bug http://adactio.com/journal/5088/. Seems to work!
Authored by @scottjehl. Props to @wilto for addressing a tilt caveat.
MIT License.
FOR LATEST, SEE https://github.com/scottjehl/iOS-Orientationchange-Fix
*/
(function(w){
var doc = w.document;
if( !doc.querySelectorAll ){ return; }
var meta = doc.querySelectorAll( "meta[name=viewport]" )[ 0 ],
initialContent = meta && meta.getAttribute( "content" ),
disabledZoom = initialContent + ", maximum-scale=1.0",
enabledZoom = initialContent + ", maximum-scale=10.0",
enabled = true,
orientation = w.orientation,
rotation = 0;
if( !meta ){ return; }
function restoreZoom(){
meta.setAttribute( "content", enabledZoom );
document.body.innerHTML = document.body.innerHTML;
enabled = true;
}
function disableZoom(){
meta.setAttribute( "content", disabledZoom );
enabled = false;
}
function checkTilt( e ){
orientation = Math.abs( w.orientation );
rotation = Math.abs( e.gamma );
if( rotation > 8 && orientation === 0 ){
if( enabled ){
disableZoom();
}
}
else {
if( !enabled ){
restoreZoom();
}
}
}
w.addEventListener( "orientationchange", restoreZoom, false );
w.addEventListener( "deviceorientation", checkTilt, false );
})( this );
@alexgibson
Copy link

Interesting!

But a quick Q: Maybe I'm missing something here when viewing the demo, but when I rotate on iOS5 the text still gets enlarged, so you don't actually take advantage of the added width in landscape? Should the aim not be to reflow?

As it stands, is this demo any different to just using width=device-width on its own?

@opi
Copy link

opi commented Jan 6, 2012

Works well for me, thanks a lot !
@alexgibson : the working demo is http://scottjehl.github.com/iOS-Orientationchange-Fix/

@alexgibson
Copy link

@opi - ah, it seems all the demo needs is a sprinkling of:

-webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; text-size-adjust: 100%;

and the body text does not resize and reflows properly - great job @scottjehl!

@alexgibson
Copy link

This does work great - very clever solution! My only reservation is that iOS5 uses geolocation for compass calibration the whole time the browser window is open. I'm not sure what effect this has on battery life, but might be worth consideration?

You can enable the status bar icon for this in:

Settings -> Location Services -> System Services -> Status Bar Icon set to On

Edit - perhaps an alternative event could be to use devicemotion instead of deviceorientation? This would mean it does not require a gyroscope (only iPhone 4+), and could be done using just raw accelerometer events.

@rwaldron
Copy link

rwaldron commented Jan 6, 2012

doc.querySelector( "meta[name=viewport]" ) === doc.querySelectorAll( "meta[name=viewport]" )[ 0 ]

:)

@davidcalhoun
Copy link

This is a clever solution, thanks for offering it up!

My only concern is that it seems to be pretty intensive - deviceorientation is getting fired constantly (see http://frontendstuff.com/javascript/deviceorientation-demo.html). This takes up CPU cycles and also drains the battery faster. It might be worth putting into a setTimeout that adds a 50-100ms (?) delay so it's not so intensive. I guess the only danger there is that the user might rotate the screen so fast that checkTilt doesn't get fired in time.

@sergiolopes
Copy link

I'm proposing another solution that aims to fix the issues people found in this one. It's a highly different approach (no gesture/accelerometer hacking).

https://github.com/sergiolopes/ios-zoom-bug-fix

@scottjehl
Copy link
Author

scottjehl commented Feb 28, 2012 via email

@sergiolopes
Copy link

@scottjehl Hi Scott! Thanks for your feedback on my approach!

The CSS3 transform doesn't impact the layout, it just scales the entire page with the original layout unchanged. And setting width to device-height mean we have to scale up content when in portrait mode. I have an alternate version of the script that works with device-width and in this case we have to scale down in landscape mode. Working with scale up is a lot easier than with scale down, but maybe I could publish the other version too and let people choose.

And I knew about your new project and its evolutions. Using devicemotion seems much better than deviceorientation! I still have some issues with it, specially when quickly rotating my iPad (couldn't reproduce on my old iPod Touch 2nd gen; its hardware is so slow that a fast rotation doesn't affect the script :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment