最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery smooth change of innerHTML - Stack Overflow

programmeradmin3浏览0评论

I have the code below working like a charm:

var div = $('#div');
div.html('<div>one line</div><div>another line</div>');
div.slideDown('slow');

But the problem comes when I need to change the content (the number of lines):

div.html('<div>one line replacement</div><div>another line replacement</div><div>third line</div>')

This transition is too fast. How to animate this?

I have the code below working like a charm:

var div = $('#div');
div.html('<div>one line</div><div>another line</div>');
div.slideDown('slow');

But the problem comes when I need to change the content (the number of lines):

div.html('<div>one line replacement</div><div>another line replacement</div><div>third line</div>')

This transition is too fast. How to animate this?

Share Improve this question edited Jul 17, 2009 at 20:33 Jader Dias asked Jul 17, 2009 at 17:29 Jader DiasJader Dias 90.5k159 gold badges434 silver badges632 bronze badges 2
  • Is the issue "too fast", or you really want to slide down line by line? – o.k.w Commented Jul 17, 2009 at 17:45
  • the issue is that it is too fast – Jader Dias Commented Jul 17, 2009 at 17:58
Add a comment  | 

5 Answers 5

Reset to default 6

You can add an invisible span to the html:

<span class="foo" style="display: none">some other lines</span>

And then fade them in:

$("span.foo").fadeIn('slow');


Going by your edit, you may also want to consider:

div.slideUp('slow'); // you may want this to be 'fast'
div.html('some other lines');
div.slideDown('slow');

A wrinkle on Daniel Sloof's answer:

div.fadeOut('fast', function() {
  div.html(html);
  div.fadeIn('fast');
}

This will ensure your first animation is complete before proceeding. In my current case at least, fading makes a nicer experience than sliding.

Maybe if you put the extra lines into a div with display:none style, then you can fade in that div, something like this (concept - code not tested):

div.html("<div id='next_line' style='display:none'>some other lines</div>");
$("#next_line").fadeIn('slow');

If you want to make it take a certain time, then:

div.slideDown(numberOfMilliseconds);

Thomas mentioned "slideDown(numberOfMilliseconds)". I have tried it and jquery's doc defined the speed in milliseconds is the time taken to execute the amimation.

In that case, 1 line or 10 lines will take the same amount of time to slide. If you know the number of line, perhaps you can add in a multiplier, e.g. slideDown(lineCount * speedInMS)

发布评论

评论列表(0)

  1. 暂无评论