Search code examples
javascripthtmlcssslideshowfade

Background slideshow (javascript) fade transition effect


I am trying to apply the fade effect to the javascsript background slideshow. When I changed the value, JS ignored it. I tried fadeIn and fadeOut. What is wrong with this code?

var bgimages=new Array()
bgimages[0]="tenis.jpg"
bgimages[1]="rtrr.jpg"
bgimages[2]="tenis.jpg"

//preload images
var pathToImg=new Array()
for (i=0;i<bgimages.length;i++) {
    pathToImg[i]=new Image()
    pathToImg[i].src=bgimages[i]
}
var inc=-1

function bgSlide() {
    if (inc<bgimages.length-1)
        inc++
    else
        inc=0

    document.body.background=pathToImg[inc].src
}

if (document.all||document.getElementById)
    window.onload=new Function('bgSlide(); setInterval("bgSlide()",3000),fade(2000)')

Solution

  • You can not fade background images. You can fade images in <img> tags.

    Here is my jsFiddle DEMO.

    Here is some sample code (including JQuery, CSS, and HTML):

    <!doctype html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Untitled Document</title>
    <style>
    img{
        position:absolute;
        top:0;
        display:none;
        width:auto;
        height:100%;
    }
    </style>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script>
    function fader() {
        $("img").first().appendTo('#wrap').fadeOut(3000);
        $("img").first().fadeIn(3000);    
        setTimeout(fader, 4200);
    }
    </script>
    </head>
    <body onLoad="fader();">
    <div id="wrap">
    <img src="http://stats.hashweb.org/static/img/jsfiddle-logo.png">
    <img src="http://startbootstrap.com/templates/freelancer/img/profile.png">
    <img src="http://www.silvercoinstoday.com//images/Silver-Element-Atomic-Structure.jpg">
    </div>
    </body>
    </html>