I am trying to make an element not clickable when it is animated. When the animation is done I want it to be clickable again. I have searched a long time for some help on how to achieve this, but I can't get it to work and I don't know why.
The HTML:
<p>
<span class="red" id="a">A</span><span id="llright" class="hide">llright</span> B C D
</p>
When letter A is clicked, it moves to the left and then some text fades in next to it.
The jQuery:
(function() {
var letterA = $('#a'),
llright = $('#llright');
$('#a:not(.open)').live('click', function() {
letterA.animate({
marginRight: "5.7in",
}, 1300, function() {
letterA.addClass('open');
llright.fadeIn(1300); // Animation complete.
});
});
$('#a.open').live('click', function() {
llright.fadeOut(700);
letterA.delay(700).animate({
marginRight: "0.0in",
}, 700, function() {
letterA.removeClass('open');
});
});
})();
The animation works great, but this doesn't:
if(letterA.is(':animated')) {
letterA.unbind('click');
};
The last part doesn't work at all, even when I insert a simple alert() instead of unbind() it doesn't seem to figure out when A is moving and not.
I could really use some help here, I have tried everything I can think of.
Thx /Oscar
Your check is only running on page load. Do the check inside of the click function:
Also, live() is deprecated; consider moving to on().
It's more economical to attach just one click handler with internal branching to cater for the different circumstances; forward animation, reverse animation and to reject while animation is in progress.
You can also get away without addClass/removeClass('open') by testing the state of
llright
instead.Something like this should do it:
Note also that with this code it is not necessary to use
.live()
.