-
-
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 ); |
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 :)
@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!