禁用 ctrl +鼠标在网页上滚动

发布于 2024-10-09 05:23:56 字数 63 浏览 2 评论 0原文

如何使用 jquery 禁用网页上的 CTRL + 鼠标滚动?

我想在我的网页上禁用放大和缩小!

How can I disable CTRL + mouse scroll on web page with jquery?

I want to disable zoom in and zoom out on my web page!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

睡美人的小仙女 2024-10-16 05:23:56

是的,您可以这样做:

$(window).keydown(function(event) 
{
    if((event.keyCode == 107 && event.ctrlKey == true) || (event.keyCode == 109 && event.ctrlKey == true))
    {
        event.preventDefault(); 
    }

    $(window).bind('mousewheel DOMMouseScroll', function(event) 
    {
        if(event.ctrlKey == true)
        {
            event.preventDefault(); 
        }
    });
});

这只适用于 Firefox、Chrome 和 Opera。它不适用于 Internet Explorer。

Yes you can do this :

$(window).keydown(function(event) 
{
    if((event.keyCode == 107 && event.ctrlKey == true) || (event.keyCode == 109 && event.ctrlKey == true))
    {
        event.preventDefault(); 
    }

    $(window).bind('mousewheel DOMMouseScroll', function(event) 
    {
        if(event.ctrlKey == true)
        {
            event.preventDefault(); 
        }
    });
});

This will only work for Firefox, Chrome and Opera. It will not work with Internet Explorer.

流年里的时光 2024-10-16 05:23:56

这是不可能的。

相反,您应该设计页面以支持缩放。

This is not possible.

Instead, you should design your page to support zoom.

你可以这样做,我在chrome、filefox和ie上测试过。效果很好

(function () {

        /**
         * Main stopscrollwheelzoom constructor
         */
        let SSWZ = function () {

            /**
             * Handler for scroll- control must be pressed.
             * @param e
             */
            this.keyScrollHandler = function (e) {
                if (e.ctrlKey) {
                    e.preventDefault();
                    return false;
                }
            }
        };

        if (window === top) {
            let sswz = new SSWZ();
            window.addEventListener('wheel', sswz.keyScrollHandler, { passive: false });
        }

    })();

you can do this, i've tested on chrome、filefox and ie. it work well

(function () {

        /**
         * Main stopscrollwheelzoom constructor
         */
        let SSWZ = function () {

            /**
             * Handler for scroll- control must be pressed.
             * @param e
             */
            this.keyScrollHandler = function (e) {
                if (e.ctrlKey) {
                    e.preventDefault();
                    return false;
                }
            }
        };

        if (window === top) {
            let sswz = new SSWZ();
            window.addEventListener('wheel', sswz.keyScrollHandler, { passive: false });
        }

    })();

与之呼应 2024-10-16 05:23:56

更新了答案,没有 Jquery,也没有副作用。

let handlingScroll = false;
    window.addEventListener('keydown', function (event) {
        if (event.ctrlKey === true && handlingScroll === true) {
            event.preventDefault();
        }
    }, {passive: false});

    window.addEventListener('wheel', function (event) {
        if (event.ctrlKey === true) {
            handlingScroll = true
            //set timeout to reset the handlingScroll variable after the scroll event is finished
            setTimeout(() => {
                handlingScroll = false
            }, 10)
            event.preventDefault();
        } else {
            handlingScroll = false
        }
    }, {passive: false});

我仅使用滚动事件就成功做到了这一点,但由于某些原因,在 Chrome 上无法阻止第一个事件缩放。无论如何,第一个 ctrl+scroll 都会缩放,直到我添加了 keydown 方法。

添加 keydown 事件使我能够防止第一个滚动事件触发缩放,但随后我产生了副作用,即所有与 ctrl 键相关的快捷方式(例如刷新等)将不再起作用。

这就是为什么我还必须添加一个变量来跟踪天气或没有正在进行的滚动事件。

最后我不得不添加一个计时器,以便 var 再次设置为 false。

结果是所有 ctrl + 滚动事件(包括第一个事件)都会阻止缩放。您可以修改它来检查 -= 是否被保留以防止键盘缩放,但这不是我的意图。

感谢 Sumesh Dubey 的回答启发我使用 keydown 事件。

Updated answer without Jquery and without the side effects.

let handlingScroll = false;
    window.addEventListener('keydown', function (event) {
        if (event.ctrlKey === true && handlingScroll === true) {
            event.preventDefault();
        }
    }, {passive: false});

    window.addEventListener('wheel', function (event) {
        if (event.ctrlKey === true) {
            handlingScroll = true
            //set timeout to reset the handlingScroll variable after the scroll event is finished
            setTimeout(() => {
                handlingScroll = false
            }, 10)
            event.preventDefault();
        } else {
            handlingScroll = false
        }
    }, {passive: false});

I was successfully doing it with the just the scroll event but for some reason on chrome there was no way to prevent the first event from zooming. The first ctrl+scroll would zoom no matter what untill I added the keydown approach.

Adding the keydown event was what allowed me to prevent the first scroll event triggering a zoom but then I had the side efffect that ALL ctrl key related shortcuts such as refresh etc would no longer work at all.

This is why I had to also add a variable to track weather or not there was a scroll event in progress.

Finally I had to add a timer so that the var was set to false again.

The result is all ctrl + scroll events including the first one would prevent zoom. You could modify this to also check if - or = is held to prevent the keyboard zoom as well but that was not my intention.

Thanks Sumesh Dubey for his answer inspired me to use the keydown event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文