'How to add smooth transitions to a very simple javascript slideshow

I have the following JavaScript to make a slideshow:

window.addEventListener('load', main, false);

var number = 1;
var interval;

function main() {
    interval = setInterval(changeDia, 3000);
}

function changeDia() {
    var img = document.getElementById("dia");
    var currentDia = img.getAttribute("src");

    if(currentDia == "style/slideshow/6.jpg") {
        number = 1;
    } else {
        number = number + 1;
    }
    img.setAttribute("src", "style/slideshow/" + number + ".jpg");
}

This code works fine, but the transitions are very rude. I would like to have the current image to fade out and the next one to fade in so there's a smooth transition. What is the easiest way? Javascript and jQuery are both good for me.

Thanks in advance,

Cedric



Solution 1:[1]

You can do it easily using jQuery without plugins:

var i = 0,               //initial index
    duration = 1000,     //animation duration
    interval = 3000;     //interval

function switchImg() {
    $("<img>")                                               //create new <img>
        .attr("src", "style/slideshow/" + (i<6?++i:(i=1,i)) + ".jpg") //set attr.
        .css("opacity", 0)                                   //hide it
        .prependTo("#wrap")                                  //add it to DOM
        .animate({                                           //fade in
            opacity: 1
        }, {
            duration: duration
        })
        .next()                                              //select current img
        .animate({                                           //fade out
            opacity: 0
        }, {
            duration: duration
        })
        .promise()
        .done(function () {                                  //remove old img
            $(this).remove();                                // when done
            setTimeout(switchImg, interval);                 //repeat
        });
}
switchImg();                                                 //start up

http://jsfiddle.net/DerekL/vzhHZ/

Don't forget to set the position porperty of the images to absolute or else it won't work.

Solution 2:[2]

Here is smooth slideshow without Jquery, use css3 & pure javascript only:

<!DOCTYPE html>
<html>
<head>
<style>
#slider-outter { overflow: hidden;}
#slider-container { width: 100%; height: 200px; position: relative; left: 0; transition: all ease .3s;}
.slide-item {height: 200px; padding: 15px;display:block; position: absolute; top: 0;}
.slide-item:nth-child(even) {background: yellow;}
.slide-item:nth-child(odd) {background: pink;}
</style>
</head>
<body>
<h1>Smooth slideshow</h1>
<div id='slider-outter'>
<div id='slider-container'>
<div class="slide-item"><h2>Section 1</h2><p>Content A</p></div>
<div class="slide-item"><h2>Section 2</h2><p>Content B</p></div>
<div class="slide-item"><h2>Section 3</h2><p>Content C</p></div>
</div><!-- #slider-container -->
</div><!-- #slider-outter -->
<script>
  var sliderContainer = document.getElementById("slider-container");
  var sliderContainerWidth = sliderContainer.offsetWidth;
  var sliderItems = sliderContainer
     .getElementsByTagName("div"); // .getElementsByClassName('slide-item')

  // Set slider item width
  for (let y = 0; y < sliderItems.length; y++) {
    sliderItems[y].style.width = sliderContainerWidth + "px";
    sliderItems[y].style.left = (y * sliderContainerWidth) + "px";
  }

  // Slider switch action
  var activeSlide = 1;
  function nextSlide(n) {
    activeSlide++;
    if (activeSlide > sliderItems.length) { activeSlide = 1; }
    let newLeftPixel = ((activeSlide - 1) * sliderContainerWidth);
    sliderContainer.style.left = "-"+ newLeftPixel + "px";
  }

  // Auto-run every specified seconds
  setInterval(() => nextSlide(activeSlide), 5000);
</script>
</body>
</html>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2