3

I have this div I want to hide (display:none) after it fades out, so I enter this:

$('.element').click(function(){
    $(this).animate({opacity:"0"},{duration:200});
    $(this).delay(200).css('display','none');
});

And I suddenly remember that delay()s don't work for css. I used to have a little setTimeout fix for this lying around but can't find it anywhere so I tried random stuff like this:

$('.element').click(function(){
    $(this).animate({opacity:"0"},{duration:200});
});
$('.element').click(function(){
    setTimeout(function(){
        $(this).css('display','none');
    },200);
});

Still doesn't work. Can someone help me out here please?

0

4 Answers 4

4

jQuery's .animate() has a callback feature, you can send an anonymous function to be executed once the animation is done:

$(this).animate({opacity:"0"},{duration:200}, function(){
   $(this).css('display','none');
});

source: jQuery animate

Sign up to request clarification or add additional context in comments.

Comments

1

A more Elegant way of doing it is by specifying a callBack function to run after the animation.

Eg:

$(this).animate({opacity:"0"},200,function(){

     $(this).css('display','none');

});

Comments

1

Use animate callback function, that function will initualise when animation complete.

Structure is like this = .animate( properties [, duration] [, easing] [, complete] )

Example:

$('.element').click(function(){
    $(this).animate({opacity:"0"},200,function() {//use callback here
       $(this).css('display','none');
    });
});

1 Comment

Probably no need to delay the css anymore. I assume this was just so that its ran after the animate
0
$('.element').fadeOut('slow', function() {
    // Animation complete. do something here if you want
 });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.