admin管理员组文章数量:1332389
What's a simple jQuery script I can load on my webpage that will auto fade between each of the three "box" divs, and repeat? I'd like it to show a div for 5 seconds while hiding the other two, then run a 1 second fade transition into the next.
The three div's are currently stacked on top of each other.
<div class="boxes">
<div class="box1">text1</div>
<div class="box2">text2</div>
<div class="box3">text3</div>
</div>
What's a simple jQuery script I can load on my webpage that will auto fade between each of the three "box" divs, and repeat? I'd like it to show a div for 5 seconds while hiding the other two, then run a 1 second fade transition into the next.
The three div's are currently stacked on top of each other.
<div class="boxes">
<div class="box1">text1</div>
<div class="box2">text2</div>
<div class="box3">text3</div>
</div>
Share
asked Apr 9, 2013 at 5:24
copyflakecopyflake
7,1675 gold badges21 silver badges14 bronze badges
1
-
The key is using the the style attributes of
position
,z-index
,display
and asetInterval
function. – asprin Commented Apr 9, 2013 at 5:29
5 Answers
Reset to default 4You could try this:
$(document).ready(function () {
var allBoxes = $("div.boxes").children("div");
transitionBox(null, allBoxes.first());
});
function transitionBox(from, to) {
function next() {
var nextTo;
if (to.is(":last-child")) {
nextTo = to.closest(".boxes").children("div").first();
} else {
nextTo = to.next();
}
to.fadeIn(500, function () {
setTimeout(function () {
transitionBox(to, nextTo);
}, 5000);
});
}
if (from) {
from.fadeOut(500, next);
} else {
next();
}
}
DEMO: http://jsfiddle/CYJLA/
I'm sure it could be cleaned up and/or be more efficient, but I just put it together to get it working.
jQuery Cycle is easy to set-up and can do what you are asking.
Here's an example: http://jsfiddle/n1ck/4PvU7/
$('.boxes').cycle({
speed: 1000, // speed of the transition
timeout: 5000 // milliseconds between slide transitions (0 to disable auto advance)
});
http://jsfiddle/Bouillou/UQTY2/52/
<div class="boxes">
<div class="fade-effect box1">text1</div>
<div class="fade-effect box2">text2</div>
<div class="fade-effect box3">text3</div>
</div>
jQuery.fn.nextOrFirst = function (selector) {
var next = this.next(selector);
return (next.length) ? next : this.prevAll(selector).last();
}
$(document).ready(function () {
$('div.fade-effect').fadeOut(500);
var fadeInTime = 1000;
var intervaltime = 5000;
//First call
setTimeout(function () {
fadeMe($('div.fade-effect').first());
}, intervaltime);
function fadeMe(div) {
div.fadeIn(fadeInTime, function () {
div.fadeOut(fadeInTime);
//Recursive call
setTimeout(function () {
fadeMe(div.nextOrFirst());
}, intervaltime);
});
}
});
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis./ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$(".box2,.box3").css("display","none");
var a=2;
function recursive()
{
$(".box1,.box2,.box3").css("display","none");
$(".box"+a).fadeIn(1000);
a=Number(a)+1;
if(a==4)
a=1;
}
setInterval(function(){recursive()},5000);
});
</script>
</head>
<body>
<div class="boxes">
<div class="box1" c>text1</div>
<div class="box2">text2</div>
<div class="box3">text3</div>
</div>
</body>
</html>
Here is one of the simplest solution:
CSS:
.boxes { position:relative; height:332px; width:500px; }
.boxes div { position:absolute; left:0; top:0; }
jQuery:
$('.boxes div:gt(0)').hide();
setInterval(function(){
$('.boxes :first-child').fadeOut().next('div').fadeIn().end().appendTo('.boxes');
}, 3000);
Working Demo
本文标签: javascriptAuto fadeshow between multiple divsStack Overflow
版权声明:本文标题:javascript - Auto fadeshow between multiple divs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742292086a2447994.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论