移动端禁止页面缩放问题

第一步:

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

第二步:

     window.onload = function () {
        document.addEventListener('gesturestart', function (e) {
            e.preventDefault();
        });
        //以上是禁止双指缩放
        //以下是禁止双击缩放
        document.addEventListener('touchstart', function (e) {
            if (e.touches.length > 1) {
                e.preventDefault();
            }
        });
        var endTouch = 0;
        document.addEventListener('touchend', function (e) {
            var nowTouch = (new Date()).getTime();
            if (nowTouch - endTouch<= 300) { //为什么是300,看注意
                e.preventDefault();
            }
            endTouch = nowTouch ;
        }, false);
    };

注意:
点击穿透问题:移动端click比touchstart/touchend会有大约300ms延迟,浏览器会在点击后等待300ms去判断是否用户多次点击,300ms内有多次点击就判定为不是单击事件。

猜你喜欢

转载自blog.csdn.net/Mrlujiao_code/article/details/113121976