移动端禁止浏览器强制缩放问题

1,112 阅读1分钟

我先在index.html中加上了下面的代码:

 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0,user-scalable=0">

发现在手机自带的浏览器中,还是能放大的,然后就在App.vue中加上了如下代码

注意:一定不要忘了{ passive: false }!!!

  mounted() {
    document.documentElement.addEventListener(
      "touchstart",
      function (event) {
        if (event.touches.length > 1) {
          event.preventDefault();
        }
      },
      { passive: false }
    );
    // 禁止双击放大
    let lastTouchEnd = 0;
    document.documentElement.addEventListener(
      "touchend",
      function (event) {
        var now = Date.now();
        if (now - lastTouchEnd <= 300) {
          event.preventDefault();
        }
        lastTouchEnd = now;
      },
      { passive: false }
    );
  },