SHARE
Facebook X Pinterest WhatsApp

Create a Simple Automated Slideshow

Written By
thumbnail
Rob Gravelle
Rob Gravelle
Jul 18, 2016

Create a Simple Automated Slideshow

I was looking to present a simple looping slideshow on my website the other day, and it just seemed that all of the plugins I came across were too bulky for my trivial purpose, so I turned my attention to stand-alone solutions. In the end, I found that jQuery was all that I needed to accomplish what I wanted to. In today’s article, I’ll show you how I did it.

Defining the Images

Some people include the image list in the JavaScript source, but I prefer to keep them within the HTML realm, since they are part of the page’s content. I was too lazy to figure out all of the image dimensions, so that is done in the JS. My original list contains a number of t-shirts with religion-themed graphics and text. Stuff like “Worship me or I Will Torture You Forever! Have Nice Day. BFF – God”. You might not think of this as being an especially funny subject, but with the right lighthearted mindset, religion can be hilarious fodder. Just ask Monty Python. Nonetheless, today’s demo slideshow is about a much less incendiary subject, namely cars. 

<div id="slideshow">
  <div>
    <img src="mustang.jpg" alt="mustang.jpg"><br>
    A newer Mustang.
  </div>
  <div>
    <img src="camaro.jpg" alt="camaro.jpg"><br>
    A modern Camaro.
  </div>
  <div>
    <img src="classic.jpg" alt="classic.jpg"><br>
    An oldie but a goodie!
  </div>
  <div>
    <img src="classic2.jpg" alt="classic2.jpg"><br>
    More old cars.
  </div>
  <div>
    <img src="video_game_car.jpg" alt="video_game_car.jpg"><br>
    A good renering of a classic race car.
  </div>
</div>

The CSS is really quite minimal and just sets the container DIV dimensions, adds padding between the top and left page margins and the image, and surrounds the image with a border and shadow.

#slideshow {
    position: relative;
    width: 300px;
    height: 510px;
    padding: 10px;
}

#slideshow > div {
    position: absolute;
    top: 10px;
    left: 10px;
}
#slideshow > div > img {
    border: 2px solid white;
    box-shadow: 0 0 20px white;
}

Appended to the bottom of the document, the JS kicks in only after all of our images have been defined. The first line hides all the image DIVs so that we can show them one at a time within the slideshow. Then, setInterval() is employed to iterate over the images at a pace of every 5 seconds. It invokes the anonymous function that fades the current image out, proceeds to the next sibling, fades it in, and appends it to the slideshow container element. Notice the use of the end() function that returns the this pointer to an earlier state. Without it, the next call to fadeOut() does not work as expected, leading to the images being piled on top of each other.

$("#slideshow > div").hide();

setInterval(function() {
  $('#slideshow > div:first')
    .fadeOut(1000)
    .next()
    .fadeIn(1000)
    .end()
    .appendTo('#slideshow');
},  5000);

Rob Gravelle

In his spare time, Rob has become an accomplished guitar player, and has released several CDs. His band, Ivory Knight, was rated as one of Canada’s top hard rock and metal groups by Brave Words magazine (issue #92) and reached the #1 spot in the National Heavy Metal charts on Reverb Nation.

Recommended for you...

The Revolutionary ES6 Rest and Spread Operators
Rob Gravelle
Aug 23, 2022
Ahead of Time (AOT) Compilation in Angular
Tariq Siddiqui
Aug 16, 2022
Converting a JavaScript Object to a String
Rob Gravelle
Aug 14, 2022
Understanding Primitive Type Coercion in JavaScript
Rob Gravelle
Jul 28, 2022
HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.