-
-
Save scottjehl/1568180 to your computer and use it in GitHub Desktop.
/* | |
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 ); |
@necolas. Ooh good call, I'm not sure, but actually I don't think we need that part anymore. Can someone test? http://jsbin.com/esitej/2
...I forgot I'd added that as a last-ditch effort to get zoom to restore in an earlier version.
Suggestion: Use doc.querySelector( "meta[name=viewport]" )
instead of doc.querySelectorAll( "meta[name=viewport]" )[ 0 ]
. document.querySelectorAll
is if you intend to use multiple elements.
Okay folks, the git repo's here: https://github.com/scottjehl/iOS-Orientationchange-Fix
The demo url's in ghpages: http://scottjehl.github.com/iOS-Orientationchange-Fix/
Article posted here: http://filamentgroup.com/lab/a_fix_for_the_ios_orientationchange_zoom_bug/
Thanks @Krinkle. Care to submit a pull? Otherwise, I'll get in there soon!
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?
Works well for me, thanks a lot !
@alexgibson : the working demo is http://scottjehl.github.com/iOS-Orientationchange-Fix/
@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!
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.
doc.querySelector( "meta[name=viewport]" ) === doc.querySelectorAll( "meta[name=viewport]" )[ 0 ]
:)
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.
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).
@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 :)
Test URL here btw http://jsbin.com/esitej/