I'm not really strong on javascript, and I'm trying to get my head around the whole animation thing. What started out as seemingly simple is perhaps going to be a little more complicated than I thought.
What I want to do is have 5 <div>s displayed in a fixed position on my page. The divs will contain text and pictures. Only one div is displayed at a time, and every 5 seconds, the next div slides over the top of the current div (from right to left) until it is completely obscured. At the conclusion of the fifth, it starts again with number 1.
Can anyone point me to some code or a tutorial that will achieve this?
Thanks
CTB
Oh Lord, please help me be the person my dog thinks I am.
#btnContainer {
position: absolute; top: 250px; left: 70px}
-->
</style>
<script type="text/javascript">
<!--
var curDiv = 0;
var divSlide;
var divId;
var curLeft = -200;
function swapDiv() {
divId = "div"+curDiv;
divSlide = setInterval("slideDiv()",30);
}
function slideDiv() {
document.getElementById(divId).style.visibility="visible";
curLeft = curLeft + 1;
if(curLeft > 0) { //we have scrolled all the way to the right
document.getElementById(divId).style.visibility="hidden";
clearInterval(divSlide);
curDiv = curDiv + 1;
if(curDiv > 3) curDiv = 0;
curLeft = -200;
swapDiv();
} else { //keep scrolling to the right
document.getElementById(divId).style.left = curLeft+"px";
document.getElementById(divId).style.top = 0+"px";
}
}
//-->
</script> </head>
<body>
<div id="main">
<div id="div0">This is Div 1</div>
<div id="div1">This is Div 2</div>
<div id="div2">This is Div 3</div>
<div id="div3">This is Div 4</div>
</div>
Two questions, if I may impose on your time a little more...
1 - Is it possible to keep the last one visible while the next one slides in over the top?
2 - Once the div has slid to position, how do I get it to pause for x seconds on that div before sliding the next one into place?
Cheers
CTB
Oh Lord, please help me be the person my dog thinks I am.
function slideDiv() {
document.getElementById(divId).style.zIndex = zedIndex; //this line has to be here and not in swapDiv() to eliminate flickering during 2nd loop through
curLeft = curLeft + 1;
if(curLeft > 0) { //we have scrolled all the way to the right
clearInterval(divSlide);
clearTimeout(pauser);
curDiv = curDiv + 1;
if(curDiv > 3) curDiv = 0;
curLeft = -200;
pauser = setTimeout("swapDiv()",5000);
} else { //keep scrolling to the right
document.getElementById(divId).style.left = curLeft+"px";
}
}
//-->
</script> </head>
<body>
<div id="main">
<div id="div0">This is Div 1</div>
<div id="div1">This is Div 2</div>
<div id="div2">This is Div 3</div>
<div id="div3">This is Div 4</div>
</div>
function slideDiv(dir) {
document.getElementById(divId).style.zIndex = zedIndex; //this line has to be here and not in swapDiv() to eliminate flickering during 2nd loop through
curLeft = curLeft + (1*dir);
if(dir == 1 && curLeft > 0) { //we have scrolled all the way to the right
clearInterval(divSlide);
clearTimeout(pauser);
curDiv = curDiv + 1;
if(curDiv > 3) curDiv = 0;
curLeft = -200 * dir;
pauser = setTimeout(function() {swapDiv(dir)},5000);
} else if (dir == -1 && curLeft < 0) { //we have scrolled all the way to the left
clearInterval(divSlide);
clearTimeout(pauser);
curDiv = curDiv + 1;
if(curDiv > 3) curDiv = 0;
curLeft = 200 * dir;
pauser = setTimeout(function() {swapDiv(dir)},5000);
} else { //keep scrolling to the right
document.getElementById(divId).style.left = curLeft+"px";
}
}
<div id="main">
<div id="div0">This is Div 1</div>
<div id="div1">This is Div 2</div>
<div id="div2">This is Div 3</div>
<div id="div3">This is Div 4</div>
</div>
<div id="btnContainer">
<button id="l2r" onclick="stopSliding(this.id);swapDiv(1);">Slide Left 2 Right</button>
<button onclick="stopSliding('stop');">Stop Sliding</button>
<button id="r2l" onclick="stopSliding(this.id);swapDiv(-1);">Slide Right 2 left</button>
</div>
Bookmarks