11

I have 1 page which has 2 DIV elements which is shown/hidden based on user click on action buttons with javascript, I would like to toggle scaling on action button click.

I tried with below javascript and it is changing viewport meta but getting no effect.

Any suggestions?

var ViewPortAllowZoom = 'width=device-width;';

var ViewPortNoZoom = 'width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no;';

  function AllowZoom(flag) {
            if (flag == true) {
                $('meta[name*=viewport]').attr('content', ViewPortAllowZoom);                
            }
            else {
                $('meta[name*=viewport]').attr('content', ViewPortNoZoom);
            }
        }
sshow
  • 8,820
  • 4
  • 51
  • 82
msqr
  • 296
  • 1
  • 2
  • 10

3 Answers3

19

Removing and re-adding the meta tag worked for me:

function AllowZoom(flag) {
  if (flag == true) {
    $('head meta[name=viewport]').remove();
    $('head').prepend('<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=10.0, minimum-scale=1, user-scalable=1" />');
  } else {
    $('head meta[name=viewport]').remove();
    $('head').prepend('<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=0" />');              
  }
}

However, if the user zooms in and then switches to no zoom, the viewport remains zoomed and the user can no longer change the zoom. Does anyone have a solution for that?

Erik Z
  • 199
  • 2
  • 3
  • 1
    content="width=960" forces it to stay at zoomed "out" on portrait view. – Caius Eugene Jan 19 '12 at 09:38
  • it doesn't work on Android 4.2 native browser, if you remove the zoom then you can't re-enable it by restoring the meta tag attributes as done in this function. – guari Dec 02 '13 at 20:55
5
$('body').bind('touchmove', function(event) { event.preventDefault() }); // turns off

$('body').unbind('touchmove'); // turns on
stealthyninja
  • 10,343
  • 11
  • 51
  • 59
-1

These are the steps to disable zooming:

  1. Convince your PM that disabling is a bad idea (see the below article).
  2. If he still wants it, convince him harder.
  3. If he still wants it, read this article and add preventDefault to gesturestart as explained in a comment in there: https://wouterdeschuyter.be/blog/how-to-disable-viewport-scaling-in-ios-10-you-dont-941140811

It works in iPhone 6 and doesn't block scrolling.

Thanks for link, Aloober: https://stackoverflow.com/a/41166167/1409261

Community
  • 1
  • 1
Szalai Laci
  • 540
  • 2
  • 5
  • 12