105

In my page I have a bunch (about 30) dom nodes that should be added invisible, and fade in when they are fully loaded.
The elements need a display: inline-block style.

I would like to use the jquery .fadeIn() function. This requires that the element initially has a display: none; rule to initially hide it. After the fadeIn() the elements off course have the default display: inherit;

How can I use the fade functionality with a display value other than inherit?

Boris Callens
  • 90,659
  • 85
  • 207
  • 305

6 Answers6

248

$("div").fadeIn().css("display","inline-block");

MakuraYami
  • 3,398
  • 3
  • 16
  • 19
40

You could use jQuery's animate function to change the opacity yourself, leaving the display unaffected.

philnash
  • 70,667
  • 10
  • 60
  • 88
11

Just to flesh out Phil's good idea, here is a fadeIn() that loads fades in each element with the class .faded in turn, converted to animate() :

Old:

$(".faded").each(function(i) {
    $(this).delay(i * 300).fadeIn();
});

New:

$(".faded").each(function(i) {
    $(this).delay(i * 300).css('opacity',0).animate({'opacity': 1}, 500);
});

Hope that help another jQuery newb like myself :) If there's a better way to do that, please tell us!

Mere Development
  • 2,439
  • 5
  • 32
  • 63
9

Makura's answer did not work for me so my solution was

<div id="div" style="display: none">Some content</div>

$('#div').css('display', 'inline-block').hide().fadeIn();

hide immediately applies display: none but before that it saves the current display value in the jQuery data cache which will be restored by the subsequent animation calls.

So the div starts statically defined as display: none. Then it is set to inline-block and immediately hidden just to be faded in back to inline-block

Clodoaldo Neto
  • 118,695
  • 26
  • 233
  • 260
2

According to the jQuery docs for .show(),

If an element has a display value of inline, then is hidden and shown, it will once again be displayed inline.

So my solution to this problem was to apply a css rule to a class display: inline-block on the element, then I added another class called "hide" which applies display: none; When I .show() on the element, it showed inline.

aug
  • 11,138
  • 9
  • 72
  • 93
amurrell
  • 2,383
  • 22
  • 26
  • I would assume that this should also work when you set `#el{display:inline-block;display:none;}` and then run `$('#el').fadeIn();` you get inline-block. But it doesn't. – Fanky Jan 23 '18 at 12:24
1

This works even with display:none preset by css. Use

$('#element').fadeIn().addClass('displaytype');

(and also $('#element').fadeOut().removeClass('displaytype');)

with setup in CSS:

#element.displaytype{display:inline-block;}

I consider this a workaround as I believe fadeIn() should work so that it would just remove the last rule - display:none when set to #element{display:inline-block;display:none;} but it is malfunctioningly removing both.

Fanky
  • 1,673
  • 1
  • 18
  • 20