• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 786
  • Last Modified:

jQuery: Animate height for dynamically added content

I want to use jQuery to animate dynamically added content. The problem is that because the content is dynamic, I cannot set the pixel height for the animation.  


If you test the code below you will see the problem.  Each time you refresh the page, the size of the added content is different.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Demo</title>

<style type="text/css">
#leadForm1 {height: 0; overflow: hidden; width: 700px;}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>

<script type="text/javascript">
/* <![CDATA[ */

function CreateText($min,max)
{
 var l= l = ('Lorem ipsum ');
 var validchars = "eir mod tem por invidunt ut lab ore dol ore magna aliqu yam erat sed diam voluptu ";
  var minsize, maxsize, count, actualsize;
  minsize = Math.floor(Math.random()*100);
  maxsize = Math.floor(Math.random()*9000);
  if (maxsize<(Math.ceil(maxsize * 1.5)))
  var str = (Math.ceil(maxsize * 1.5));
  actualsize = Math.floor(Math.random() * (maxsize - minsize + 1)) + minsize;
    for (count = 1; count<actualsize; count++)
    l=l+validchars.charAt(Math.floor(Math.random()* validchars.length));
  return l;
}

$(document).ready(function(){

$('#rq1').click(function() {
  $('#leadForm1').html(CreateText());
  $('#leadForm1').animate({height: '650px'}, 900);
  $(this).css( {visibility: "hidden"})
  return false;
});

});

/* ]]> */ 
</script> 

</head>
<body>

<p><a href="#" id="rq1">Click Here to create random text.</a></p>

<div id="leadForm1"></div>
<hr />
<h1>Hello World</h1>
</body>
</html>

Open in new window

0
hankknight
Asked:
hankknight
1 Solution
 
David S.Commented:
Try using slideDown() instead of animate(): http://api.jquery.com/slideDown/
0
 
Gurvinder Pal SinghCommented:
check this one also,
http://stackoverflow.com/questions/1263789/jquery-animate-slide-to-height-100

with the same logic you can find the innerheight of the element you want to vertically scroll, and set that as an option to the animate function of jquery
0
 
mickey159Commented:
Do you really need the slide effect?
If not, try the following code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Demo</title>

<style type="text/css">
#leadForm1 {height: 0; overflow: hidden; width: 700px; height: auto;}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>

<script type="text/javascript">
/* <![CDATA[ */

function CreateText($min,max)
{
 var l= l = ('Lorem ipsum ');
 var validchars = "eir mod tem por invidunt ut lab ore dol ore magna aliqu yam erat sed diam voluptu ";
  var minsize, maxsize, count, actualsize;
  minsize = Math.floor(Math.random()*100);
  maxsize = Math.floor(Math.random()*9000);
  if (maxsize<(Math.ceil(maxsize * 1.5)))
  var str = (Math.ceil(maxsize * 1.5));
  actualsize = Math.floor(Math.random() * (maxsize - minsize + 1)) + minsize;
    for (count = 1; count<actualsize; count++)
    l=l+validchars.charAt(Math.floor(Math.random()* validchars.length));
  return l;
}
$(document).ready(function(){
$('#rq1').click(function() {
  $('#leadForm1').html(CreateText());

  $(this).hide();
  return false;
});

});

/* ]]> */ 
</script> 

</head>
<body>

<p><a href="#" id="rq1">Click Here to create random text.</a></p>

<div id="leadForm1"></div>
<hr />
<h1>Hello World</h1>
</body>
</html>

Open in new window

0
 
hankknightAuthor Commented:
Thanks, slideDown does the job.  Here is the working code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Demo</title>

<style type="text/css">
#leadForm1 {display: none; width: 700px;}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
</head>
<body>

<p><a href="#" id="rq1">Click Here to create random text.</a></p>

<div id="leadForm1"></div>
<hr />
<h1>Footer</h1>

<script type="text/javascript">
/* <![CDATA[ */

function CreateText($min,max)
{
 var l= l = ('Lorem ipsum ');
 var validchars = "eir mod tem por invidunt ut lab ore dol ore magna aliqu yam erat sed diam voluptu ";
  var minsize, maxsize, count, actualsize;
  minsize = Math.floor(Math.random()*100);
  maxsize = Math.floor(Math.random()*9000);
  if (maxsize<(Math.ceil(maxsize * 1.5)))
  var str = (Math.ceil(maxsize * 1.5));
  actualsize = Math.floor(Math.random() * (maxsize - minsize + 1)) + minsize;
    for (count = 1; count<actualsize; count++)
    l=l+validchars.charAt(Math.floor(Math.random()* validchars.length));
  return l + '.';
}

$(document).ready(function(){
 $('#rq1').click(function() {
  $('#leadForm1').html(CreateText());
  $('#leadForm1').slideDown(500);
  $(this).css( {visibility: "hidden"})
  return false;
 });
});

/* ]]> */ 
</script> 

</body>
</html>

Open in new window

0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now