0

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>
Community
  • 1
  • 1
Ionut Necula
  • 11,107
  • 4
  • 45
  • 69
  • The code works well, why are you wanting to disable click? – KyleM Mar 10 '16 at 15:21
  • you can simply use a flag and test it. On click, if no 'li' is animating (flag false), animate it and position the flag to true. When animation is complete, position the flag to false. This way, only one animation at a time can occur. – TCHdvlp Mar 10 '16 at 15:23
  • @KyleM, the code works partially well, the next click should work only after the animation has finished. – Ionut Necula Mar 10 '16 at 15:25
  • @TCHdvlp, can you help me with an actual answer? please. – Ionut Necula Mar 10 '16 at 15:26
  • https://jsfiddle.net/TCHdevlp/tzmf2311/ I lightened it up. The 'flag' I mentionned already exists, it's the `active` class. Here is the asynchronous version (I preffer this one) https://jsfiddle.net/TCHdevlp/tzmf2311/1/ – TCHdvlp Mar 10 '16 at 15:33
  • @TCHdvlp, I think you missunderstood my question. – Ionut Necula Mar 10 '16 at 15:39

1 Answers1

1

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>
jacksbox
  • 911
  • 1
  • 11
  • 24
  • Partially, now the click does not work on the active class when animating. I need the same thing for all list items. – Ionut Necula Mar 10 '16 at 15:59