TechGlider
03-24-2009, 08:01 PM
I am trying to incorporate animation with a toggle feature.. Can someone help me do this..
If you look, i am using an array to loop through so that any previous toggled 'on' div will return to hidden. But i am trying to get the animation with it now, so i was wondering if this is something i will have to modify with the js or the html? It will work as is, but without the animation functions below..
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<script type="text/javascript">
var timerlen = 5;
var slideAniLen = 250;
var timerID = new Array();
var startTime = new Array();
var obj = new Array();
var endHeight = new Array();
var moving = new Array();
var dir = new Array();
function displayDiv(showDiv)
{
var myDivs = new Array("faq1","faq2","faq3","faq4");
for (myDiv in myDivs){
//hide all
document.getElementById(myDivs[myDiv]).style.display = 'none';
}
//show desired
document.getElementById(showDiv).style.display = 'block';
}
function slidedown(showDiv){
if(moving[showDiv])
return;
if(document.getElementById(showDiv).style.display != "none")
return; // cannot slide down something that is already visible
moving[showDiv] = true;
dir[showDiv] = "down";
startslide(showDiv);
}
function slideup(showDiv){
if(moving[showDiv])
return;
if(document.getElementById(showDiv).style.display == "none")
return; // cannot slide up something that is already hidden
moving[showDiv] = true;
dir[showDiv] = "up";
startslide(showDiv);
}
function startslide(showDiv){
obj[showDiv] = document.getElementById(showDiv);
endHeight[showDiv] = parseInt(obj[showDiv].style.height);
startTime[showDiv] = (new Date()).getTime();
if(dir[showDiv] == "down"){
obj[showDiv].style.height = "1px";
}
obj[showDiv].style.display = "block";
timerID[showDiv] = setInterval('slidetick(\'' + showDiv + '\');',timerlen);
}
function slidetick(showDiv){
var elapsed = (new Date()).getTime() - startTime[showDiv];
if (elapsed > slideAniLen)
endSlide(showDiv)
else {
var d =Math.round(elapsed / slideAniLen * endHeight[showDiv]);
if(dir[showDiv] == "up")
d = endHeight[showDiv] - d;
obj[showDiv].style.height = d + "px";
}
return;
}
function endSlide(showDiv){
clearInterval(timerID[showDiv]);
if(dir[showDiv] == "up")
obj[showDiv].style.display = "none";
obj[showDiv].style.height = endHeight[showDiv] + "px";
delete(moving[showDiv]);
delete(timerID[showDiv]);
delete(startTime[showDiv]);
delete(endHeight[showDiv]);
delete(obj[showDiv]);
delete(dir[showDiv]);
return;
}
</script>
<style type="text/css">
div {
display: none;
background: lightblue;
}
</style>
</head>
<body>
<p><h1>Frequently Asked Questions</h1></p><br>
<a href="#" onclick="displayDiv('faq1');">This is FAQ question number one?</a><br>
<div id="faq1" style="overflow: auto;width:450px; border:2px ridge black;">This is the content to FAQ number one. This is the content to FAQ number one. This is the content to FAQ number one. This is the content to FAQ number one. </div>
<a href="#" onclick="displayDiv('faq2')">This is FAQ question number two?</a><br>
<div id="faq2" style="overflow: auto; auto;width:600px;border:2px ridge black;">This is the content to FAQ number two. This is the content to FAQ number two. This is the content to FAQ number two. This is the content to FAQ number two. This is the content to FAQ number two. This is the content to FAQ number two. This is the content to FAQ number two. </div>
<a href="#" onclick="displayDiv('faq3')">This is FAQ question number three.</a><br>
<div id="faq3" style="overflow: auto;border:2px ridge black;">This is the content to FAQ number three?</div>
<a href="#" onclick="displayDiv('faq4')">This is FAQ question number four.</a><br>
<div id="faq4" style="overflow: auto;width:500px;border:2px ridge black;">This is the content to FAQ number four? This is the content to FAQ number four? This is the content to FAQ number four? This is the content to FAQ number four? </div>
<p><br><br><br>I created a few different ways they can look. If you want me to do something different let me know.</p>
</body>
</html>
If you look, i am using an array to loop through so that any previous toggled 'on' div will return to hidden. But i am trying to get the animation with it now, so i was wondering if this is something i will have to modify with the js or the html? It will work as is, but without the animation functions below..
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<script type="text/javascript">
var timerlen = 5;
var slideAniLen = 250;
var timerID = new Array();
var startTime = new Array();
var obj = new Array();
var endHeight = new Array();
var moving = new Array();
var dir = new Array();
function displayDiv(showDiv)
{
var myDivs = new Array("faq1","faq2","faq3","faq4");
for (myDiv in myDivs){
//hide all
document.getElementById(myDivs[myDiv]).style.display = 'none';
}
//show desired
document.getElementById(showDiv).style.display = 'block';
}
function slidedown(showDiv){
if(moving[showDiv])
return;
if(document.getElementById(showDiv).style.display != "none")
return; // cannot slide down something that is already visible
moving[showDiv] = true;
dir[showDiv] = "down";
startslide(showDiv);
}
function slideup(showDiv){
if(moving[showDiv])
return;
if(document.getElementById(showDiv).style.display == "none")
return; // cannot slide up something that is already hidden
moving[showDiv] = true;
dir[showDiv] = "up";
startslide(showDiv);
}
function startslide(showDiv){
obj[showDiv] = document.getElementById(showDiv);
endHeight[showDiv] = parseInt(obj[showDiv].style.height);
startTime[showDiv] = (new Date()).getTime();
if(dir[showDiv] == "down"){
obj[showDiv].style.height = "1px";
}
obj[showDiv].style.display = "block";
timerID[showDiv] = setInterval('slidetick(\'' + showDiv + '\');',timerlen);
}
function slidetick(showDiv){
var elapsed = (new Date()).getTime() - startTime[showDiv];
if (elapsed > slideAniLen)
endSlide(showDiv)
else {
var d =Math.round(elapsed / slideAniLen * endHeight[showDiv]);
if(dir[showDiv] == "up")
d = endHeight[showDiv] - d;
obj[showDiv].style.height = d + "px";
}
return;
}
function endSlide(showDiv){
clearInterval(timerID[showDiv]);
if(dir[showDiv] == "up")
obj[showDiv].style.display = "none";
obj[showDiv].style.height = endHeight[showDiv] + "px";
delete(moving[showDiv]);
delete(timerID[showDiv]);
delete(startTime[showDiv]);
delete(endHeight[showDiv]);
delete(obj[showDiv]);
delete(dir[showDiv]);
return;
}
</script>
<style type="text/css">
div {
display: none;
background: lightblue;
}
</style>
</head>
<body>
<p><h1>Frequently Asked Questions</h1></p><br>
<a href="#" onclick="displayDiv('faq1');">This is FAQ question number one?</a><br>
<div id="faq1" style="overflow: auto;width:450px; border:2px ridge black;">This is the content to FAQ number one. This is the content to FAQ number one. This is the content to FAQ number one. This is the content to FAQ number one. </div>
<a href="#" onclick="displayDiv('faq2')">This is FAQ question number two?</a><br>
<div id="faq2" style="overflow: auto; auto;width:600px;border:2px ridge black;">This is the content to FAQ number two. This is the content to FAQ number two. This is the content to FAQ number two. This is the content to FAQ number two. This is the content to FAQ number two. This is the content to FAQ number two. This is the content to FAQ number two. </div>
<a href="#" onclick="displayDiv('faq3')">This is FAQ question number three.</a><br>
<div id="faq3" style="overflow: auto;border:2px ridge black;">This is the content to FAQ number three?</div>
<a href="#" onclick="displayDiv('faq4')">This is FAQ question number four.</a><br>
<div id="faq4" style="overflow: auto;width:500px;border:2px ridge black;">This is the content to FAQ number four? This is the content to FAQ number four? This is the content to FAQ number four? This is the content to FAQ number four? </div>
<p><br><br><br>I created a few different ways they can look. If you want me to do something different let me know.</p>
</body>
</html>