I have a div
I want to fade out, update its content, and then fade back in. So far I have tried:
$('#myDivID').fadeOut('slow', function() {
$('#myDivID').replaceWith("<div id='myDivID'>" + content + "</div>");
$('#myDivID').fadeIn('slow');
});
What happens is that the fade out completes and calls the anonymous callback. So far, so good.
The div
's content is replaced correctly, but the fadeIn()
effect is immediate — no smooth transition, just a quick, snappy jump to the updated div
.
Is there a better way to accomplish this? Thanks for your advice.
The jQuery fadeOut() method is used to fade out a visible element. Syntax: $(selector). fadeOut(speed,callback);
You can use the jQuery fadeIn() and fadeOut() methods to display or hide the HTML elements by gradually increasing or decreasing their opacity.
You should do it this way (this works, is tested code):
$('#myDivID').fadeOut('slow', function() {
$('#myDivID').html(content);
$('#myDivID').fadeIn('slow');
});
Your code wasn't working because the new created div is instantly visible. Another solution is to add a display:none
like the following:
$('#myDivID').fadeOut('slow', function() {
$('#myDivID').replaceWith("<div id='myDivID' style='display:none'>" + content + "</div>");
$('#myDivID').fadeIn('slow');
});
Hope this helps Cheers
use SetTimeOut
setTimeout(function() { $('#myDivID').fadeIn('slow'); }, 5000);
this works
jsFiddle http://jsfiddle.net/3XYE6/1/
$('#doit').click(function(){
$('#myDivID').fadeOut('slow', function() {
$('#myDivID').html('New content in MyDiv ('+Math.random()+')')
setTimeout(function() { $('#myDivID').fadeIn('slow'); }, 5000);
});
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With