简体   繁体   中英

How to disable click while animating in jQuery?

I'm trying to disable click while an list element is animated but with no success. I've found posts like this , this or this and others on SO, followed their examples, but nothing seems to work. Can anyone help me with this please?

My code:

 var animateStatus = false; $('li').on('click', function(){ animateStatus = true; $('li.active').animate({ 'top': '0px' }, 300, function(){ animateStatus = false; }); $('li.active').removeClass('active'); $(this).addClass('active'); $(this).animate({ 'top': '-5px' }, 300, function(){ animateStatus = false; }); }); 
 ul{ display: inline-block; vertical-align: middle; height: 300px; } li{ position: relative; display: inline-block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li>test 1</li> <li>test 2</li> <li>test 3</li> </ul> 

is this your desired behaviour?

 var animateStatus = false; $('li').on('click', function(){ if(animateStatus) return; animateStatus = true; $('li.active').animate({ 'top': '0px' }, 300); $('li.active').removeClass('active'); $(this).addClass('active'); $(this).animate({ 'top': '-5px' }, 300, function(){ animateStatus = false; }); }); 
 ul{ display: inline-block; vertical-align: middle; height: 300px; } li{ position: relative; display: inline-block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li>test 1</li> <li>test 2</li> <li>test 3</li> </ul> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM