简体   繁体   English

jQuery-mousedown,mouseup并单击同一元素

[英]JQuery - mousedown,mouseup and click on same element

I have a carousel and I need to make him work as Instagram carousel does. 我有一个轮播,我需要让他像Instagram轮播一样工作。 On click change slide, but on mousedown just stop animation. 在单击更改幻灯片时,但在鼠标按下时停止动画。 My JQuery : 我的jQuery:

 $(".fancy-carousel").on('mousedown',function (e) {
    ...stop animation
});

$(".fancy-carousel").on('mouseup',function (e) {
  ..continue animation
});

$(".fancy-carousel").on('click',function (e) {
   ..change slide
});

But i don´t know how can i let script know about difference between "click" and "mousedown". 但是我不知道如何让脚本知道“ click”和“ mousedown”之间的区别。 When i click on element and hold for a time, it stop animation but after "mouseup" it trigger "click" event too. 当我单击元素并保持一段时间时,它会停止动画,但在“鼠标”操作后也会触发“点击”事件。 Is there any way how to split this events? 有什么办法可以拆分此事件? Or should i do it with some calculating of mouse hold time? 还是应该通过一些鼠标保持时间来计算?

A “click” is just a full cycle of a “mousedown” and a “mouseup”. “点击”只是“鼠标按下”和“鼠标向上”的整个周期。 You can't have one without the other. 你不能没有一个。

For your code to know the difference, you'll need a variable that tracks your intentions. 为了让您的代码了解不同之处,您需要一个变量来跟踪您的意图。

  1. Create a variable to track your intention - default it to “click”. 创建一个变量以跟踪您的意图-默认为“点击”。
var intention = "click";
  1. In your mousedown function, pause the animation and start a timer. 在鼠标按下功能中,暂停动画并启动计时器。 We will use this timer to detect how long the mouse is down for (Ie, if it's for over a second, it's not a click and you just want to trigger mouseup) 我们将使用此计时器来检测鼠标按下的时间(即,如果持续一秒钟以上,则不是单击,您只想触发鼠标向上)
var detectIntention = setTimeout(function(){
    intention = "mouseup";
})
  1. In your mouse up function, cancel this timeout. 在鼠标上移功能中,取消此超时。 If mouse up is called after just a few MS, then you want to do a click. 如果仅在几个MS之后调用了鼠标上移,则您要单击。
clearTimeout(detectIntention);

if (intention === "mouseup") {
  // do mouseup stuff
}
// reset intention
intention = click;
  1. Check in your click function that you wanted to do a click; 签入您想要点击的点击功能;
if (intention === "click") {
  // do click stuff
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM