7

I have this weird behavior with .fadeIn()

If I write

$('#MyDiv').show();

The div shows just fine.

However, if I write

$('#MyDiv').fadeIn(400);

The div shows but with opacity 0!

The line before is:

$('#MyDiv').hide()
    .html(TheHTML)
    .css('top', 0);

UPDATE: if I write $('#MyDiv').show(400);

The div also stays at opacity 0.

thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
frenchie
  • 51,731
  • 109
  • 304
  • 510
  • 1
    if an element has opacity:0 === is not visible. But you say: "**The div shows but with opacity 0!**" I really don't understand – Roko C. Buljan Jun 01 '12 at 06:21
  • Where does `TheHTML` come from ? The problem certainly comes from somewhere else, like maybe your div is empty so its height and width is equal to 0 or something like that – romainberger Jun 01 '12 at 06:23

4 Answers4

6

Some CSS is overriding your div when it is hidden..Better add the opacity style to your div as 1.

Else do,

$('#MyDiv').css('opacity','1');

After your fadeIn call...

Henry Howeson
  • 677
  • 8
  • 18
Kabilan S
  • 1,104
  • 4
  • 15
  • 31
6

Try jQuery .fadeTo function.

.fadeTo( duration, opacity [, callback] )  
.fadeTo( duration, opacity [, easing] [, callback] )  

Sample:

$('#book').fadeTo('slow', 0.5, function() {
  // Animation complete.
});
Tooraj Jam
  • 1,592
  • 14
  • 27
2

The following will make it visible as you want

$('#MyDiv').animate({
  opacity: 1
},400);

You can try following:

$('#MyDiv').fadeIn(400, function(){
    $(this).css('opacity', 1)
});

DEMO

But I think your should also work just fine. See here

David Titarenco
  • 32,662
  • 13
  • 66
  • 111
thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
0

Check if you have a

opacity: 0 !important;

//or

opacity: 0;

In your css in that case you have to change opacity manually yourself like this

$('#MyDiv').fadeIn(400,function(){
   $(this).css('opacity',1);
});

Check this fiddle. First div will never appear.

Prasenjit Kumar Nag
  • 13,391
  • 3
  • 45
  • 57