Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 how do I get the click to go the full distance first before the click can be used again? The issue is if the user rapidly clicks on the arrow button it resets the distance and sometimes can end up in the middle of an image instead of right at the seam. What code am I missing to accomplish this?

$(function () {  

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

});
like image 495
Keith Avatar asked Dec 11 '22 11:12

Keith


1 Answers

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/

like image 88
Ben Jackson Avatar answered Dec 14 '22 01:12

Ben Jackson