Replace images with time intervals using jquery

Last updated on February 25th, 2022 at 06:25 am

Click to rate this tutorial!
[Total: 45 Average: 3.7]

Replace images with time intervals using Jquery

This post will show you how to replace an image in specific time intervals.

For example: replacing image1.jpg with image2.jpg every 5 seconds.

Using setInterval() we are triggering a call to swapImages() function every 5000 milliseconds.

So, before we continue, we need to define where our images are coming from. Images URLs can be stored in javascript array or we could choose more elegant way and simply read them from HTML document.

We have this HTML code with div id “myGallery”

<div id="myGallery">
    <img src="image1.jpg" class="active" />
    <img src="image2.jpg" />
    <img src="image3.jpg" />
  </div>

We now need to hide all images and overlay all of them on one another. Here is a CSS to do that:

#myGallery{
      position:relative;
      width:400px; /* Set your image width */
      height:300px; /* Set your image height */
    }
    #myGallery img{
      display:none;
      position:absolute;
      top:0;
      left:0;
    }
    #myGallery img.active{
      display:block;
    }

Now, lets write jQuery function that will fade out currently active image and fade in the next image. Here is jQuery code:

function swapImages(){
      var $active = $('#myGallery .active');
      var $next = ($('#myGallery .active').next().length > 0) ? $('#myGallery .active').next() : $('#myGallery img:first');
      $active.fadeOut(function(){
      $active.removeClass('active');
      $next.fadeIn().addClass('active');
      });
    }

And now whats left is to add setInterval() with our function and the time interval we want our image to fade out and fade in.

// Run our swapImages() function every 5secs
setInterval('swapImages()', 5000);

Now we have gallery/slideshow with images changing every 5 seconds. You can easily customize this jQuery script to create your own slideshow.

<html>
<head>
  <script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
<script>
    function swapImages(){
      var $active = $('#myGallery .active');
      var $next = ($('#myGallery .active').next().length > 0) ? $('#myGallery .active').next() : $('#myGallery img:first');
      $active.fadeOut(function(){
      $active.removeClass('active');
      $next.fadeIn().addClass('active');
      });
    }

    $(document).ready(function(){
      // Run our swapImages() function every 5secs
      setInterval('swapImages()', 5000);
    })
  </script>
  <style>
    #myGallery{
      position:relative;
      width:400px; /* Set your image width */
      height:300px; /* Set your image height */
    }
    #myGallery img{
      display:none;
      position:absolute;
      top:0;
      left:0;
    }
    #myGallery img.active{
      display:block;
    }
  </style>
</head>
<body>
  <div id="myGallery">
<img height=450px src="image1.jpg" alt="" class="active" />
    <img height=450px src="image2.jpg" alt="" />
    <img height=450px src="image3.jpg" alt="" />
  </div>
</body>
</html>

In the HTML code above I have given height=450px to make sure that all the images are shown with similar size.

Demo

Click to rate this tutorial!
[Total: 45 Average: 3.7]

3 replies on “Replace images with time intervals using jquery”

the code you had was throwing an error in firebug but the following code works 🙂

function swapImages(){
var $active = $(‘#myGallery .active’);
var $next = ($(‘#myGallery .active’).next().length > 0) ? $(‘#myGallery .active’).next() : $(‘#myGallery img:first’);
$active.fadeOut(function(){
$active.removeClass(‘active’);
$next.fadeIn().addClass(‘active’);
});
}

$(document).ready(function(){
setInterval(‘swapImages()’, 5000);
});

Leave a Reply

Your email address will not be published.