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

前端 未结 6 2007
温柔的废话
温柔的废话 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

    Use .off() to unbind the click as soon as it occurs, then re-bind it once the animation completes.

    function go(elem){
        $(elem).off('click'); console.log(elem);
        var dir = elem.id == "right" ? '+=' : '-=';
        $(".outerwrapper").stop().animate({ left: dir + '251' }, 3000, function(){
            $("#right, #left").click(go);
        });
    }
    $("#right, #left").click(function () {
        go(this);
    });
    

    jsFiddle example

    You can see in this simplified example that the click event is unbound immediately after clicking, and then rebound once the animation completes.

提交回复
热议问题