快速点击会导致幻灯片视图无法正常工作?

时间:2013-11-12 15:38:50

标签: javascript jquery html css slide

这是工作演示。 http://jsfiddle.net/Evqqp/1/

请查看演示以轻松了解该问题。快速点击箭头,你会看到视图搞砸了。

据我所知,这可能是因为我所做的300ms动画。什么是处理点击的干净方式,这样它不会弄乱视图。我可以使用标志来检查之前的点击操作是否完成。但如果有更好的方法,我想寻求意见。

我在做动画的代码

$(".rightArrow").on("click", function () {
    if ((Math.abs(parseInt($(".slideBox").css("margin-left"))) + $(".mainDiv").width()) < $(".slideBox").width()) {
        $(".slideBox").animate({
            "margin-left": parseInt($(".slideBox").css("margin-left")) - $(".mainDiv").width()
        }, 300, checkRightArrow);
        $(".leftArrow").show();
    } else {
        $(".rightArrow").hide();
    }
});

谢谢

4 个答案:

答案 0 :(得分:1)

尝试.stop(true,true)

$(".slideBox").stop(true,true).animate({

答案 1 :(得分:1)

每当使用动画时,您应该始终stop()元素上的上一个动画,然后再重新制作动画。

$(".slideBox").stop(true, true).animate(...

http://jsfiddle.net/Evqqp/4/

答案 2 :(得分:1)

您需要添加

event.stopPropagation();

后:

$(".rightArrow").on("click", function () {

这样:

$(".rightArrow").on("click", function () {
     event.stopPropagation();
     ... 

答案 3 :(得分:1)

使用以下

检查您的元素当前是否已设置动画
if(!$('#myElement').is(':animated'))
{
    // Do your animation here
}