Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of David S.
David S.
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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

Avatar of hankknight

ASKER

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