Forum Moderators: open
What I'm looking to do is to create some code to where if a user clicks on some text, a div expands from a height of 100px to a height of 200px.
It's simple enough, I just need to create a function that sets the height of the object to my second height. However, instead of just "jumping" from 100 to 200px, I would like the box to smoothly transition or "roll out" to the new height.
How would I go about doing this? Would I create 100 steps for a new height of 101px, 102px 103px... 199px, 200px, each with a fraction of a second delay between?
Is there a better way to do this?
Thanks!
Something like this (untested)
Javascript
<script type="text/javascript">
var intervalID = 0;
var originalSize = 100;
var newSize = 200;function startInterval(){
intervalID = setInterval ( "enlarge()", 1 );
}function enlarge(){
document.getElementById("myDiv").style.height = originalSize + 'px';
originalSize = originalSize + 5; // <-- bigger the # the faster it will growif (originalSize >= newSize){
// stops the loop
clearInterval(intervalID);
}
}
</script>
Html
<div style="border:1px solid red; width:100px; height:100px;" id="myDiv">
<a href="#" onclick="startInterval(); return false;">enlarge</a>
</div>
Tonight, I'll be able to try out the code.
Last night, I did see some library effects. Something like SPRY and Moo.fx. Those libraries are massive in size.
I've always been concious of my file size, trying to make my site as fast as I can for the end user. Will going through a simple loop give me the same effect as from one of those libraries, without all that bloated code? Or do those libraries do something more complex than a loop for their accordian effect?
Thanks