0

I'm looking for some help on achieving the following:

when document ready ..

locate and hide a specified div from DOM, while it's faded / display is set to 'none', add a class of 'img2'. After class is appended to initial selector, fade div back in. (display:block)

How can I chain these following methods in order specified?

Currently, any 'addClass' method, is initiated as soon as DOM is ready instead of after chained events.

/*
select div, 
wait 2 secs., 
fade out for 1, 
add class 'img2' (which changes background-image property / style), 
re-display (display:block) selected element. (#bg-img).
*/
$('#bg-img').delay(2000).fadeOut(1000).addClass('img2');

2 Answers 2

6

jQuery.fadeOut supports a callback for code that should be applied after the effect has been completed:

$('#bg-img').delay(2000).fadeOut(1000, function() {
  $(this).addClass('img2');
});

Update

As @mdm quite rightly points out, you can pass a callback argument to all of the jQuery animation effects.

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

2 Comments

This applies for the other animation methods too - fadeTo, fadeIn, slideToggle, slideUp, and slideDown.
This is perfect. Exactly what I'm looking for - (I should have known this.) Thank you for your time. Cheers!
2

try with

$('#bg-img').delay(2000).fadeOut(1000, function () {
   $(this).addClass('img2');
 });

1 Comment

Thank you @Fader, as mentioned above, exactly what was needed. Cheers!

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.