How to fade to display: inline-block

谁说胖子不能爱 提交于 2019-11-26 14:29:56

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

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

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!

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

amurrell

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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!