我是靠谱客的博主 高兴寒风,这篇文章主要介绍JS限制网页缩放比例,现在分享给大家,希望可以做个参考。

通过JS控制页面缩放比例,只允许放大到200%和缩小到100%,源码如下:

const 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限制网页缩放比例内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(121)

评论列表共有 0 条评论

立即
投稿
返回
顶部