当前位置: 首页 > news >正文

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

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

const keyCodeMap = {// 91: true, // command61: true,107: true, // 数字键盘 +109: true, // 数字键盘 -173: true, // 火狐 - 号187: true, // +189: true, // -};

二、覆盖ctrl||command + ‘+’/‘-’

// 覆盖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) { // Firefoxevent.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 });

四、完整代码示例

/*** 采用覆盖键、覆盖鼠标滑动事件;禁止通过	ctrl + +/- 和 	ctrl + 滚轮 对页面进行缩放* */
window.onload = function () {/*** 兼容电脑端的禁止通过	ctrl + +/- 和 	ctrl + 滚轮 对页面进行缩放* */const keyCodeMap = {// 91: true, // command61: 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) { // Firefoxevent.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();});
};
http://www.lryc.cn/news/449302.html

相关文章:

  • 某客户Oracle RAC无法启动故障快速解决
  • 【计算机网络 - 基础问题】每日 3 题(二十八)
  • 探索甘肃非遗:Spring Boot网站开发案例
  • 产品管理- 互联网产品(6):产品测试
  • 奖金高达 110 万元,Spatial Joy 2024 全球 AR 应用开发大赛启动
  • git add成功后忘记commit的文件丢了?
  • Python Web 开发中的DevOps 实践与自动化运维
  • 探索私有化聊天软件:即时通讯与音视频技术的结合
  • 性能调优知识点(mysql)三
  • TinyWebSever项目面试题整理
  • 维修保养记录接口-维修保养记录API-汽车接口
  • 基于 RealSense D435相机实现手部姿态检测
  • linux 下mailx 的使用。发送短信
  • 把网易云音乐的网页源码复制出来,粘贴在hbuilder中,运行于浏览器,为什么没有任何内容显示?
  • excel怎么转换json
  • 二、认识大模型
  • 2024年【电工(高级)】考试题及电工(高级)考试内容
  • Unity中分辨率适配
  • 图像处理基础知识点简记
  • 微信小程序-使用vant组件库
  • Java【注解】
  • 基于安卓开发大型体育场管理系统的设计与实现(源码+定制+讲解)
  • 【Go】-Websocket的使用
  • 怎么查看员工电脑安装了什么软件
  • 面积开运算bwareaopen
  • TortoiseGit 下载和安装
  • 0x09 瑞友 应用虚拟化系统 GetBSAppUrl SQL注入漏洞 - 复现
  • C++(Qt)软件调试---内存调试器Dr.Memory(21)
  • Python3自带HTTP服务:轻松开启与后台管理
  • 传统行业选择企业大文件传输系统需要注意哪些?