禁止缩放

43 阅读1分钟

记一次禁止缩放

通用(必须写)

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

有的浏览器生效,有的浏览器不生效,但是仍然需要保留;

CSS全部禁止

效果:滑动、缩放都会被禁止

html {
  touch-action: manipulation;
  user-select: none;
  -ms-content-zooming: none;
  touch-action: none;
}

js代码禁止

效果:只会禁止缩放,仍然可以滑动 问题: ios会有问题,频繁快速点击 + 双指缩放仍然会出现缩放的情况

// 禁止多指操作
document.documentElement.addEventListener(
  "touchstart",
  function (event) {
    if (event.touches.length > 1) {
      event.preventDefault();
    }
  },
  false
)
// 记录点击间隔,以此来判断快速点击缩放问题
let lastTouchEnd = 0;
document.documentElement.addEventListener(
  "touchend",
  function (event) {
    const now = Date.now();
    if (now - lastTouchEnd <= 300) {
      event.preventDefault();
    }
    lastTouchEnd = now
  },
  false
)

// 禁止ios的多指操作,ios特有事件
document.addEventListener('gesturestart', function (e) {
  e.preventDefault()
})

结尾

我认为 js + meta 就可以了,如果有其他的方案请指教一下。