多个div之间自动淡入/显示

时间:2013-04-09 05:24:13

标签: javascript jquery

我可以在我的网页上加载一个简单的jQuery脚本,它会在三个“box”div之间自动淡出,并重复一遍?我希望它显示一个div 5秒,同时隐藏其他两个,然后运行1秒淡入淡出过渡到下一个。

这三个div目前相互叠加。

<div class="boxes">

<div class="box1">text1</div>
<div class="box2">text2</div>
<div class="box3">text3</div>

</div>

5 个答案:

答案 0 :(得分:4)

你可以试试这个:

$(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.net/CYJLA/

我确信它可以清理和/或提高效率,但我只是把它放在一起让它运转起来。

答案 1 :(得分:2)

jQuery Cycle易于设置,可以按照您的要求进行操作。

这是一个例子: http://jsfiddle.net/n1ck/4PvU7/

$('.boxes').cycle({
    speed: 1000, // speed of the transition
    timeout: 5000 // milliseconds between slide transitions (0 to disable auto advance) 
});

答案 2 :(得分:0)

http://jsfiddle.net/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);
        });
    }
});

答案 3 :(得分:0)

    <!DOCTYPE html>
    <html>
    <head>
    <script src="//ajax.googleapis.com/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>

答案 4 :(得分:-2)

这是最简单的解决方案之一:

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