使用Javascript启用/禁用iPhone Safari的缩放功能?

msq*_*sqr 12 javascript iphone mobile-safari

我有1页有2个DIV元素显示/隐藏基于用户点击操作按钮与javascript,我想切换操作按钮单击缩放.

我尝试使用下面的javascript,它正在改变viewport meta,但没有任何效果.

有什么建议?

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);
            }
        }
Run Code Online (Sandbox Code Playgroud)

小智 19

删除并重新添加元标记对我有用:

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" />');              
  }
}
Run Code Online (Sandbox Code Playgroud)

但是,如果用户放大然后切换到无缩放,则视口将保持缩放状态,用户无法再更改缩放.有人有解决方案吗?


小智 7

$('body').bind('touchmove', function(event) { event.preventDefault() }); // turns off

$('body').unbind('touchmove'); // turns on
Run Code Online (Sandbox Code Playgroud)