简体   繁体   中英

jQuery FadeIn and FadeOut animation show white color inbetween

I have change the background image of div every 5 seconds to make it look nices i am adding image to div as background . My script adds white color in between image transition which make it look bad, i tried to fix it but not much seems to work. Below code snippet now shows image using FadeIn effect if i add FadeOut to this then it addes white between transition.

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())

    });
}

I am not sure how to fix this so it will work image FadeOut & FadeIn between images

Fiddle http://jsfiddle.net/7xxx6ah3/5/

use this

        <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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM