执行鼠标移动时的鼠标按住事件

发布于 2024-12-29 10:04:55 字数 588 浏览 1 评论 0原文

我已经下载了一个用于鼠标按住事件的 JQuery 插件。

http://remysharp.com/2006/12/15/jquery-mousehold- event/

我有一个调用 mousemove 事件的 div:

div.addEventListener('mousemove',function() {
//things
});

每当我像这样调用 mousehold 事件时:

var value = 0;
$(div).mousehold(function() {
value += 20;
$(div).html(value);
});

它就会起作用。但是,如果我在 onmousehold 时开始移动(因此调用 mousemove 事件),则该值不再增加,这意味着,即使我的左键单击仍然保持,它也会停止调用 mousehold 事件。

如何才能使鼠标移动时,鼠标按住事件仍然有效?恩克斯!

I've downloaded a JQuery plugin for a mousehold event.

http://remysharp.com/2006/12/15/jquery-mousehold-event/

I have this div that calls for mousemove event:

div.addEventListener('mousemove',function() {
//things
});

whenever I call the mousehold event like this:

var value = 0;
$(div).mousehold(function() {
value += 20;
$(div).html(value);
});

it would work. But if I start moving (thus, calling the mousemove event) while onmousehold, the value does not increase anymore, meaning, it stopped calling the mousehold event even if I got my left click still on hold.

How can I make it happen that when I do a mousemove, the mousehold event still works? tnx!

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

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

发布评论

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

评论(2

猫九 2025-01-05 10:04:55

您所问的问题(至少我认为您所问的问题)很简单,具有一些基本逻辑。

通过绑定 mousedown、mousemove 和 mouseup 并为鼠标的“按下”状态设置标志,可以轻松完成此操作: JSFiddle

What you're asking (at least, what I think you're asking), is simple enough with some basic logic.

By binding the mousedown, mousemove, and mouseup and setting a flag for the "down" state of the mouse, this can easily be done: JSFiddle

倾听心声的旋律 2025-01-05 10:04:55

您可以像这样使用事件对象:

div.addEventListener('mousemove', function(event) {

  // `event.buttons` gets the state of the mouse buttons
  // 0 = not pressed, 1 = left click pressed, 2 = right click pressed
  if (event.buttons === 1) {
    value += 20;
  }

});

You could use the event object like so:

div.addEventListener('mousemove', function(event) {

  // `event.buttons` gets the state of the mouse buttons
  // 0 = not pressed, 1 = left click pressed, 2 = right click pressed
  if (event.buttons === 1) {
    value += 20;
  }

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