Prevent click event in jQuery triggering multiple times

后端 未结 6 599
甜味超标
甜味超标 2020-12-10 15:01

I have created a jQuery content switcher. Generally, it works fine, but there is one problem with it. If you click the links on the side multiple times, multiple pieces of c

6条回答
  •  伪装坚强ぢ
    2020-12-10 15:31

    Add a variable to use as a lock rather than is(:animating).

    On the click, check if the lock is set. If not, set the lock, start the process, then release the lock when the fadeIn finishes.

    var blockAnimation = false;
    
    $('#tab-list li a').click(
        function() {
            if(blockAnimation != true){
            blockAnimation = true;
            var targetTab = $(this).attr('href');
            if ($(targetTab).is(':hidden')) {
                $('#tab-list li').removeClass('selected');
                var targetTabLink = $(this).parents('li').eq(0);
                $(targetTabLink).addClass('selected');
                $('.tab:visible').fadeOut('slow',
                    function() {
                        $(targetTab).fadeIn('slow', function(){ blockAnimation=false; });
                    }
                );
            }
            }
            return false;
        }
    );
    

提交回复
热议问题