js采用覆盖键、覆盖鼠标滑动事件实现禁止网页通过 ctrl + +/- 和 ctrl + 滚轮 对页面进行缩放

一、兼容电脑端的禁止通过 ctrl + +/- 和 ctrl + 滚轮 对页面进行缩放

javascript 复制代码
const keyCodeMap = {
        // 91: true, // command
        61: true,
        107: true, // 数字键盘 +
        109: true, // 数字键盘 -
        173: true, // 火狐 - 号
        187: true, // +
        189: true, // -
    };

二、覆盖ctrl||command + '+'/'-'

javascript 复制代码
// 覆盖ctrl||command + '+'/'-'
    document.onkeydown = function (event) {
        const e = event || window.event;
        const ctrlKey = e.ctrlKey || e.metaKey;
        if (ctrlKey && keyCodeMap[e.keyCode]) {
            e.preventDefault();
        } else if (e.detail) { // Firefox
            event.returnValue = false;
        }
    };

三、覆盖鼠标滑动

javascript 复制代码
// 覆盖鼠标滑动
    document.body.addEventListener('wheel', (e) => {
        if (e.ctrlKey) {
            if (e.deltaY < 0) {
                e.preventDefault();
                return false;
            }
            if (e.deltaY > 0) {
                e.preventDefault();
                return false;
            }
        }
    }, { passive: false });

四、完整代码示例

javascript 复制代码
/**
 * 采用覆盖键、覆盖鼠标滑动事件;禁止通过	ctrl + +/- 和 	ctrl + 滚轮 对页面进行缩放
 * */
window.onload = function () {
    /**
     * 兼容电脑端的禁止通过	ctrl + +/- 和 	ctrl + 滚轮 对页面进行缩放
     * */
    const keyCodeMap = {
        // 91: true, // command
        61: true,
        107: true, // 数字键盘 +
        109: true, // 数字键盘 -
        173: true, // 火狐 - 号
        187: true, // +
        189: true, // -
    };
    // 覆盖ctrl||command + '+'/'-'
    document.onkeydown = function (event) {
        const e = event || window.event;
        const ctrlKey = e.ctrlKey || e.metaKey;
        if (ctrlKey && keyCodeMap[e.keyCode]) {
            e.preventDefault();
        } else if (e.detail) { // Firefox
            event.returnValue = false;
        }
    };
    // 覆盖鼠标滑动
    document.body.addEventListener('wheel', (e) => {
        if (e.ctrlKey) {
            if (e.deltaY < 0) {
                e.preventDefault();
                return false;
            }
            if (e.deltaY > 0) {
                e.preventDefault();
                return false;
            }
        }
    }, { passive: false });



    /**
     * 兼容iOS10以上Safari浏览器无法禁止缩放的解决方案
     * */
    // 阻止双击放大
    var lastTouchEnd = 0;
    document.addEventListener('touchstart', function (event) {
        if (event.touches.length > 1) {
            event.preventDefault();
        }
    });
    document.addEventListener('touchend', function (event) {
        var now = (new Date()).getTime();
        if (now - lastTouchEnd <= 300) {
            event.preventDefault();
        }
        lastTouchEnd = now;
    }, false);

    // 阻止双指放大
    document.addEventListener('gesturestart', function (event) {
        event.preventDefault();
    });
};
相关推荐
炬火初现6 分钟前
Qt 的原理及使用(1)——qt的背景及安装
开发语言·qt
gaoenyang7605257 分钟前
QT Creator配置Kit
开发语言·qt
浪裡遊22 分钟前
Typescript中的对象类型
开发语言·前端·javascript·vue.js·typescript·ecmascript
杨-羊羊羊30 分钟前
什么是深拷贝什么是浅拷贝,两者区别
开发语言·前端·javascript
发呆的薇薇°34 分钟前
在vue里,使用dayjs格式化时间并实现日期时间的实时更新
前端·javascript·vue.js
m0_6278275238 分钟前
vue3中 input 中放大镜在后面
javascript·vue.js·elementui
Cuit小唐40 分钟前
C++ 组合模式详解
开发语言·c++·组合模式
正在走向自律1 小时前
Python 自动化脚本开发秘籍:从入门到实战进阶(6/10)
开发语言·python
从味书1 小时前
安装typescript时,npm install -g typescript报错
javascript·typescript·npm
代码AC不AC1 小时前
【C++】模板初阶
开发语言·c++·学习分享·技术交流·模板初阶