Javascript 防止鼠标中键滚动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5136845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:47:35  来源:igfitidea点击:

Prevent middle mouse click scrolling

javascriptjqueryscrollmouseclick-event

提问by elwyn

I'm looking for a way to stop the middle mouse click from causing the browser to start scrolling, and showing the little scroll 'compass'.

我正在寻找一种方法来阻止鼠标中键单击导致浏览器开始滚动并显示小滚动“指南针”。

I have seen Disabling middle click scrolling with javascripthowever the solution is a bit more hackey than I would like, and doesn't seem like something I could actually use.

我已经看到使用 javascript 禁用中间点击滚动,但是该解决方案比我想要的要复杂一些,并且似乎不是我可以实际使用的东西。

I'm looking for a more definitive "This is how you do it" or "You cannot do that, son".

我正在寻找一个更明确的“这就是你这样做的方式”或“你不能那样做,儿子”。

I am of course open to hacks and workarounds.

我当然愿意接受黑客攻击和解决方法。

Just because S.O. questions look nicer with code, here is what I am using to close tooltips when right or middle clicking.

仅仅因为 SO 问题在代码中看起来更好,这就是我在右键或中键单击时用来关闭工具提示的内容。

msg.mousedown(function(e) {
    if (e.which == 2) {   //middle mouse click
        msg.hide();
        e.preventScrolling();   //if only this worked...
    }
    else if (e.which == 3) {   //right mouse click
        msg.hide();
    }
}).bind('contextmenu', function(e) {
    e.preventDefault();
}).click(function(e) {
    e.stopPropagation();
});

edit: jQuery, JavaScript, whatever, let's just all play nicely now :)

编辑:jQuery、JavaScript 等等,让我们现在都玩得很好:)

Edit 2:

编辑2:

I'm more interested in preventing the little scroll 'compass' than stopping the page from scrolling. I guess that wasn't very clear from my initial description.

与阻止页面滚动相比,我对防止小滚动“指南针”更感兴趣。我想这从我最初的描述中不是很清楚。

回答by pimvdb

Use:

用:

$('body').mousedown(function(e){if(e.button==1)return false});

This works on Chrome: http://jsfiddle.net/PKpBN/3/

这适用于 Chrome:http: //jsfiddle.net/PKpBN/3/

回答by rnevius

There's no need to include jQuery just for this.

没有必要为此包含 jQuery。

If you are using jQuery, there are already some great answers here. If not, you can use vanilla JS:

如果您使用 jQuery,这里已经有一些很好的答案。如果没有,您可以使用 vanilla JS:

document.body.onmousedown = function(e) { if (e.button === 1) return false; }

回答by Alper Aydingül

tested with the current version of firefox and chrome

使用当前版本的 Firefox 和 chrome 进行测试

document.body.onmousedown = function(e) {
    if(e.button == 1) {
        e.preventDefault();
        return false;
    }
}

回答by Chris McFarland

Try using return false;instead of e.preventScrolling();

尝试使用return false;代替e.preventScrolling();

回答by Rajmahendra

document.body.style.overflow=allowScroll?"":"hidden";

回答by Shadow Wizard is Ear For You

If you want to stop scrolling completely, here is the required code:

如果你想完全停止滚动,这里是所需的代码:

window.onscroll = function() {
    document.body.scrollTop = 0;
}

This will effectively disable the middle button as well..

这也将有效地禁用中间按钮。