javascript - Need onclick to go full distance before click works again

前端 未结 6 2004
温柔的废话
温柔的废话 2021-01-19 10:20

I have this javascript function I use that when clicked goes a certain distance. This is used within a scroller going left to right that uses about 7 divs. My question is ho

6条回答
  •  离开以前
    2021-01-19 10:57

    I would've thought that the easiest way would be to have a boolean flag indicating whether or not the animation is taking place:

    $(function () {
    
        var animating = false,
            outerwrap = $(".outerwrapper");
    
        $("#right, #left").click(function () {
            if (animating) {return;}
            var dir = (this.id === "right") ? '+=' : '-=';
            animating = true;
            outerwrap.animate({
                scrollLeft: dir + '251'
            }, 1000, function () {
                animating = false;
            });
        });
    
    });
    

    works for me: http://jsfiddle.net/BYossarian/vDtwy/4/

提交回复
热议问题