jQuery FadeIn和FadeOut动画之间显示白色

时间:2015-03-18 11:26:32

标签: javascript jquery html css

我每隔5秒更改一次div的背景图像,使其看起来很清楚我将图像添加到div background。我的脚本在图像转换之间添加白色,这使它看起来很糟糕,我试图修复它,但似乎没有多少工作。下面的代码片段现在使用FadeIn效果显示图像,如果我将FadeOut添加到此,则它会在转换之间加上白色。

var imgSrcs = [
    "../img/bg1.jpg",
    "../img/bg2.jpg",
    "../img/bg3.jpg",
    "../img/bg4.jpg",
    "../img/bg5.jpg"
];

var index = 1;
$('.intro').css("background-image", "url(" + imgSrcs[0] + ")")
$('.intro').fadeIn('slow', animateBackground());

function animateBackground() {
    window.setTimeout(function () {
        var url = imgSrcs[index];
        index++;
        if (index == 5)
            index = 0;
        $('.intro').delay(5000).fadeIn(1000, function () {
            $(this).css("background-image", "url(" + url + ")")
        }).fadeIn(1000, animateBackground())

    });
}

我不知道如何解决这个问题,因此它可以运行图像FadeOut& FadeIn图像之间

小提琴http://jsfiddle.net/7xxx6ah3/5/

1 个答案:

答案 0 :(得分:0)

使用它   

        <script type="text/javascript">
            $('#background_cycler').hide();//hide the background while the images load, ready to fade in later
        </script>

        <img class="active" src="images/img1.jpg" alt=""/>
        <img src="images/img2.jpg" alt=""   />
        <img src="images/img3.jpg" alt=""  />
        <img src="images/img4.jpg" alt=""/>     
        <img src="images/img5.jpg" alt=""  />
    </div>
    <style>

        #background_cycler{padding:0;margin:0;width:100%;position:absolute;top:0;left:0;z-index:-1}
        #background_cycler img{position:absolute;left:0;top:0;width:100%;z-index:1}
        #background_cycler img.active{z-index:3}

    </style>
    <script>
        function cycleImages() {
            var $active = $('#background_cycler .active');
            var $next = ($('#background_cycler .active').next().length > 0) ? $('#background_cycler .active').next() : $('#background_cycler img:first');
            $next.css('z-index', 2);//move the next image up the pile
            $active.fadeOut(1500, function () {//fade out the top image
                $active.css('z-index', 1).show().removeClass('active');//reset the z-index and unhide the image
                $next.css('z-index', 3).addClass('active');//make the next image the top one
            });
        }

        $(window).load(function () {
            $('#background_cycler').fadeIn(1500);//fade the background back in once all the images are loaded
            // run every 7s
            setInterval('cycleImages()', 7000);
        })

    </script>