使用javascript禁用中间点击滚动 [英] Disabling middle click scrolling with javascript

查看:124
本文介绍了使用javascript禁用中间点击滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我正在创建一个让人想起 whenisgood.net 的表格,它具有用于表格元素的单击和拖动切换。当左,中,右鼠标按钮激活mousedown事件时,我想调用不同类型的切换代码。

Background: I am creating a table reminiscent of whenisgood.net, in that it has click-n-drag toggling for table elements. I want to call different types of toggling code when the left, middle, and right mouse buttons activate a mousedown event.

通过使用JQuery,我有一个好的开始。

By using JQuery, I'm off to a good start.

$(".togglable").bind("contextmenu", function() {return false;});
$(".togglable").bind("mousedown", function(e){
  e.preventDefault();
  toggle(this, e);
});

toggle()函数中,我可以使用 e.which 来确定点击的按钮。

In the toggle() function I can use e.which to determine what button was clicked.

妙语:I使用e.preventDefault()希望它会停止滚动的中间单击默认行为。它没有。 如何停止激活滚动操作?

The punchline: I used e.preventDefault() hoping that it would stop the middle click default behavior of scrolling. It didn't. What can I do to stop the scroll action from activating?

另请参阅使用中间点击触发onclick事件

推荐答案

目前,我的解决方案是:(更多jquery!)

Currently, my solution is this: (more jquery!)

$(".togglable").wrap(
  "<a href='javascript:void(0);'
  onclick='return false;'></a>"
);

将其包装在链接中(通过 jquery wrap ),浏览器认为它是一个链接,即使你拖动鼠标也不要在中间点击滚动。通过这种设置和我的情况,有一些(次要的)陷阱。

By wrapping it in a link (via jquery wrap), browsers think it's a link and don't scroll on middle click, even if you drag your mouse around. With this setup, and my situation, there are a couple (minor) gotchas.

当你点击中间点时,Firefox会打开一个新选项卡,但前提是你没有拖动。当您单击中间,拖动或不拖动时,Opera将打开一个新选项卡。这就是为什么我使用 href ='javascript:void(0);'而不仅仅是 href ='#' - - 客户端的浏览器不会加载整个页面,只是一个带有奇怪网址的空白页面。

Firefox will open a new tab when you middle click, but only if you don't drag. Opera will open a new tab when you middle click, drag or not. That's why I used href='javascript:void(0);' instead of just href='#'--so that the client's browser wouldn't load a whole page, just a blank page with a strange url.

但这个解决方案就像Chrome和Safari上的魅力一样。它适用于IE8,除了现在当我左键单击并拖动时,它将指针更改为无法执行该操作符号,因为它认为我想将链接拖到某处。在较早版本的IE上未经测试。

But this solution works like a charm on Chrome and Safari. It works well with IE8, except that now when I left-click-n-drag, it changes the pointer to a "can't do that" symbol, since it thinks I want to drag the link somewhere. Untested on older versions of IE.

这篇关于使用javascript禁用中间点击滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆