通过JS控制页面缩放比例,只允许放大到200%和缩小到100%,源码如下:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36const keyCodeMap = { // 91: true, // command 61: true, 107: true, // 数字键盘 + 109: true, // 数字键盘 - 173: true, // 火狐 - 号 187: true, // + 189: true, // - }; // 覆盖[ctrl]command + [+]/[-] document.onkeydown = function (event) { //ratio = window.outerWidth / window.innerWidth; 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.addEventListener('wheel', (e) => { if (e.ctrlKey|| e.metaKey) { if (e.deltaY < 0) { document.getElementsByTagName('body')[0].style.zoom=2; e.preventDefault(); return false; } if (e.deltaY > 0) { document.getElementsByTagName('body')[0].style.zoom=1; e.preventDefault(); return false; } } }, { passive: false });
最后
以上就是高兴寒风最近收集整理的关于JS限制网页缩放比例的全部内容,更多相关JS限制网页缩放比例内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复