javascript 连续鼠标点击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6596432/
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-10-25 21:13:36  来源:igfitidea点击:

Continuous mouse click event

javascripthtmljavascript-events

提问by Tapas Bose

Is there any event generated by continuous mouse click i.e.,not releasing the mouse button 1?If no, please let me know.

是否有连续鼠标点击产生的事件,即不释放鼠标按钮 1?如果没有,请告诉我。

Thanks and regards.

谢谢并恭祝安康。

回答by James Allardice

The mousedownevent is triggered when the mouse button is pressed down. If you are looking for an event that fires repeatedly, while the button is held down, you are out of luck, but you can use the mousedownevent to repeatedly perform an action, and stop when the mouseupevent is triggered.

mousedown当鼠标按钮被按下时触发该事件。如果你正在寻找一个重复触发的事件,当按钮被按住时,你就不走运了,但你可以使用该mousedown事件重复执行一个动作,并在mouseup事件触发时停止。

For example, you could use the setIntervalfunction to repeatedly call a function while the mouse button is down, and then use clearIntervalto stop when the mouse button is released. Here is an example (using jQuery):

例如,您可以使用该setInterval函数在鼠标按下时重复调用一个函数,然后clearInterval在松开鼠标按钮时使用该函数停止。这是一个示例(使用 jQuery):

var interval;
$("#elementToClick").mousedown(function() {
    interval = setInterval(performWhileMouseDown, 100);
}).mouseup(function() {
    clearInterval(interval);  
});
function performWhileMouseDown() {
    $("#output").append("<p>Mouse down</p>");
}

You can see this running in this example fiddle.

您可以在这个示例 fiddle 中看到它正在运行。

回答by pai.not.pi

There is a JQuery plugin: LongClick

有一个JQuery 插件:LongClick

Longclick is press & hold mouse button "long click" special event for jQuery 1.4.x.

Longclick 是 jQuery 1.4.x 的按住鼠标按钮“长按”的特殊事件。

The event is triggered when the mouse button stays pressed for a (configurable) number of seconds, while the pointer is stationery.

当鼠标按钮保持按下(可配置的)秒数,而指针静止不动时,将触发该事件。

回答by Paul Butcher

According to the spec,

根据规范

A click is defined as a mousedown and mouseup over the same screen location. The sequence of these events is: mousedown, mouseup, click

单击定义为在同一屏幕位置上按下鼠标和按下鼠标。这些事件的顺序是:mousedown、mouseup、click

So no, there isn't a "continuous click", because a click is a descrete event resulting from a sequence of actions.

所以不,没有“连续点击”,因为点击是由一系列动作产生的离散事件。

What you probably want to do, is receive mousedown, set a timer, and if neither mouseup or mousemove occur within some time, invoke some behaviour.

您可能想要做的是接收 mousedown,设置一个计时器,如果在一段时间内没有发生 mouseup 或 mousemove,则调用一些行为。

回答by Blue Hearthstone

Yes, you can do this using onmousemove= movefunction(event):

是的,您可以使用onmousemove= movefunction(event)以下方法执行此操作:

What I did to solve this is the following:

我为解决此问题所做的工作如下:

First, create a onmousedown()event that sets a global variable to 1when triggered.

首先,创建一个onmousedown()事件,将全局变量设置为1触发时。

Second, create a onmouseup()event that sets that global variable to 0when triggered.

其次,创建一个onmouseup()事件,将该全局变量设置为0触发时。

Then, use the onmousemove()event to trigger in the div where I want the mouse down behavior to occur but only if the global variable we set earlier is set to 1.

然后,使用该onmousemove()事件在我希望发生鼠标按下行为的 div 中触发,但前提是我们之前设置的全局变量设置为1.

example on how to use onmousemove(): http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onmousemove

关于如何使用的示例onmousemove()http: //www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onmousemove

Done.

完毕。

回答by Francesco

There is not such event.

没有这样的事件。

What you might implement to achieve this is a function that evaluates the time elapsed between the (first) mouse click ond the following mouse release.

为了实现这一点,您可能会实现一个函数,该函数评估(第一次)鼠标单击和下一次鼠标释放之间经过的时间。

Given a predefined range you can estabilish how long should the button be clicked before being considered valid in your logic.

给定一个预定义的范围,您可以确定在您的逻辑中认为按钮有效之前应该单击多长时间。