Using jQuery To Animate Inserting Text Into Div

亡梦爱人 提交于 2019-12-12 10:13:58

问题


Is it possible with jQuery to use animate, to fill a div with content smoothly, i.e. animate the height of the div growling larger as I insert content?

For example (HTML):

 <div id="my-div"></div>

Then (JavaScript):

 $("#my-div").html("some new really long string of text which makes the div height increase.");

I would like to animate the insert of the html, and thus the height increase.


回答1:


If you want to truly animate the text in, you would have to slowly append the text, one letter at a time. You can get a similar effect by using what others have mentioned already, .slideDown()

http://jsfiddle.net/rkw79/fCAVp/

$("#my-div")
    .hide()
    .html("some new really long string of text ...")
    .slideDown('slow');



回答2:


Maybe you could use a combination of the css attributes overflow:hidden; height:... and jQuery's slideDown() function.

Insert the text into a fixed-height div, determine the new height and animate it.




回答3:


The answer is very simple: adjust the height automatically and very slowly as you drop more text in the div:

$('#thedivid').append('<p>'+newtextbeingadded+'</p>').animate( { 'height':'auto' },5000 );

That should, probably, do the trick. You might need to adjust the 5000 accordingly.

Also, chances are that a vertical scroll bar would appear so you would want to make the 'thedivid' with overflow:hidden;

Good luck!




回答4:


you can use slideDown() and slideUp().

something like this:

$("#my-div").slideUp('slow', function() { $("#my-div").html("some new really long string of text which makes the div height increase."); }).slideDown('slow');

Edit:

you may want to increase the div height smoothly, to do so you can use animate() and height() this way.

I also created a FadeIn sample.



来源:https://stackoverflow.com/questions/8047284/using-jquery-to-animate-inserting-text-into-div

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