Check if element is in the fadeIn or fadeOut state ('action')
I need to stop(cancel) current .fadeIn(5000) and start the same fadeIn fro开发者_Python百科m the beginning, once a button is clicked (otherwise next fadeIn will start only after 5000). How can I check, if div is actively fading and how to can cancel it (I assume I could just .hide())?
Use .stop()
. You don't need to test if fadeIn
is running, if it is not, stop
has not effect.
You still have to hide it though. So it would be something like:
$('selector').stop(true).hide().fadeIn(5000);
You could just use .hide()
to stop it from fading and instantly hide it.
If you need to detect that it is fading without stopping the fade, it gets more complicated.
The easiest way to detect it without changing it is to use .data
to set a value on the element that shows that it is currently fading.
Then, if your .fade
call, use a callback that removes that value, like this:
element.data('fading', true);
element.fade(5000, function() {
$(this).data('fading', false);
});
You can then check if it is currently fading using:
if(element.data('fading'))
精彩评论