Skip to content

Instantly share code, notes, and snippets.

@pichfl
Created May 9, 2012 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pichfl/2644538 to your computer and use it in GitHub Desktop.
Save pichfl/2644538 to your computer and use it in GitHub Desktop.
Mobile Safari Rotation Bug Fix
// by @pichfl, inspired by the work of @adactio, @mathias, @cheeaun, @jdalton and @scottjehl
if (/iPhone|iPad|iPod/.test(navigator.platform) && navigator.userAgent.indexOf('AppleWebKit') > -1) {
(function(w){
var d = w.document,
meta,
initialContent,
content = 'content',
ms = ',minimum-scale=0.5,maximum-scale=',
enabled,
to,
qs = 'querySelector',
sa = 'setAttribute',
ael = 'addEventListener';
if( !d[qs] ){ return; }
meta = d[qs]('meta[name=viewport]');
if( !meta ){ return; }
initialContent = meta && meta.getAttribute(content);
function clearDisableTimer() {
clearTimeout(to);
}
function enableZoom() {
clearDisableTimer();
meta[sa](content, initialContent + ms + '10');
}
function disableZoom() {
meta[sa](content, initialContent + ms + '1');
}
function disable() {
clearDisableTimer();
to = setTimeout(disableZoom,600);
}
w[ael]('gesturestart',enableZoom,false);
w[ael]('gestureend',disable,false);
w[ael]('touchcancel',disable,false);
disableZoom();
}(this));
}
/*
How it works
The proposed fix by my predecessors stops working after the first gesture.
My kinda dirty fix is to set a timeout at the end of a gesture, that disables zooming again.
The threshold is big enough for most users to try zooming again (which is needed, as the first gesture will fail with that fix).
The script is optimized for compression by YUI Compressor, not for readability.
If you can, do the browser-sniffing server side and deliver that script to Mobile Safari only.
*/
// by @pichfl, inspired by the work of @adactio, @mathias, @cheeaun, @jdalton and @scottjehl
if(/iPhone|iPad|iPod/.test(navigator.platform)&&navigator.userAgent.indexOf('AppleWebKit')>-1){(function(o){var k=o.document,p,a,i="content",c=",minimum-scale=0.5,maximum-scale=",j,n,l="querySelector",m="setAttribute",e="addEventListener";if(!k[l]){return}p=k[l]("meta[name=viewport]");if(!p){return}a=p&&p.getAttribute(i);function h(){clearTimeout(n)}function f(){h();p[m](i,a+c+"10")}function b(){p[m](i,a+c+"1")}function g(){h();n=setTimeout(b,600)}o[e]("gesturestart",f,false);o[e]("gestureend",g,false);o[e]("touchcancel",g,false);b()}(this))};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment