html中Ctrl+滚轮时想禁用放大和缩小事件,怎样禁用?
试了下resize没好使啊。
<!DOCTYPE html>
<html>
<head>
<title>Disable Zoom</title>
<style>
.a {
width: 300px;
height: 300px;
background: #f00;
}
</style>
</head>
<body>
<div class="a"></div>
</body>
</html>
回复
1个回答
test
2024-06-26
很容易找到1.pc端,原生
document.addEventListener('mousewheel', function (e) {
e = e || window.event;
if ((e.wheelDelta && event.ctrlKey) || e.detail) {
event.preventDefault();
}
}, { capture: false, passive: false});
document.addEventListener('keydown', function (event) {
if ((event.ctrlKey === true || event.metaKey === true)
&& (event.keyCode === 61 || event.keyCode === 107
|| event.keyCode === 173 || event.keyCode === 109
|| event.keyCode === 187 || event.keyCode === 189)) {
event.preventDefault();
}
}, false);
2.vue 页面
methods: {
keepRatio () {
var ratio = 0; // 定义一个缩放比例
var screen = window.screen; // 获取窗口对象
var ua = navigator.userAgent.toLowerCase();
if (window.devicePixelRatio !== undefined) {
ratio = window.devicePixelRatio; // 像素大小的比例
}
else if (ua.indexOf('msie')) {
if (screen.deviceXDPI && screen.logicalXDPI) {
ratio = screen.deviceXDPI / screen.logicalXDPI;
}
}
else if (window.outerWidth !== undefined && window.innerWidth !== undefined) {
ratio = window.outerWidth / window.innerWidth; // 外部比例/内部比例:缩放比例
}
if (ratio) {ratio = Math.round(ratio * 100)}
this.ratio = (ratio / 100).toFixed(2);
document.body.style.zoom = 1 / this.ratio; // 窗口视图除以缩放比例:即缩放还原
}
}
mounted () {
this.keepRatio()
window.addEventListener('resize', () => {//监听窗口缩放
this.keepRatio()
});
}
回复
适合作为回答的
- 经过验证的有效解决办法
- 自己的经验指引,对解决问题有帮助
- 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
- 询问内容细节或回复楼层
- 与题目无关的内容
- “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容