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();
    });
};
相关推荐
yufei-coder1 小时前
掌握C#核心概念:类、继承、泛型等
服务器·开发语言·c#
john_hjy2 小时前
8. Bug 与 Error
javascript·bug
安冬的码畜日常2 小时前
【玩转 JS 函数式编程_003】1.3 JavaScript 是函数式编程语言吗?
开发语言·javascript·ecmascript·functional·vanillajs
姚*鸿的博客2 小时前
electron出现乱码和使用cmd出现乱码
前端·javascript·electron
Fenderisfine2 小时前
shadcn-vue 快速入门(2)
前端·javascript·vue.js
It'sMyGo2 小时前
Javascript数组研究03_手写实现_fill_filter_find_findIndex_findLast_findLastIndex
前端·javascript·typescript
@qike2 小时前
【C++】——类和对象(上)
java·开发语言·jvm·数据结构·c++·笔记·算法
iQM752 小时前
X-Spreadsheet使用教程:打造你的Web端电子表格应用
前端·javascript·arcgis
不惑_2 小时前
最佳ThreeJS实践 · 实现赛博朋克风格的三维图像气泡效果
javascript·node.js·webgl