Image and Text SlideShow In JavaScript

Written by
Published on
Modified on

Last time I made a quick tic tac toe clone and today, at the request of a friend, I will make an image/text slideshow widget. We see them all over the webs nowadays. Normally looking something like this.

Previous Next

Image/Text Slideshow

Name: Image/Text Slideshow
Language: JavaScript
Estimated Completion: 1 hour

You hit next and the next image loads, and you press previous and you traverse back one. This is going to be a bit different though. It needs to handle both images and text. And while I'm at it, maybe I'll make it flexible enough to handle anything else we can throw at it, like links and html and videos maybe.

First off I'll start by creating a few variables to hold my content and to keep track of which item we're currently in.

var current = 0;
var items = new Array();

Those should be it. I don't forsee myself needing to use anything else. Next up, I'll need to load the items that will go into the slideshow. I'll create a function that will populate the items array that we just declared.

Because I need to handle both images and text, I'll also store the type of content that I'm pushing into the array as well.

function loadGallery()
    var item1 = {type:"text", data:"hello world"};
    var item2 = {type: "image", data:""};
    var item3 = {type:"text", data:"goodbye"};
    var item4 = {type:"imgae", data:""};

The best way I see to keep track of multiple types of content, is to create an object for each item and store the type along with it. So I'm going to populate my items array with a collection of objects. I'm going to call this function on pageload to get the items array ready and set to go.

Next up I'll take care of the next and previous buttons. This is pretty simple and I think I'll only need to increment the current variable or decrement it depending on whether I'm going back or forward. Because I want this to be an infinite scrolling slideshow, I won't worry about disabling any button once the limit is reached. If we get to the end, the next button will simply go to the beginning, and if we're at the beginning, the previous button will go to the last element in the array.

function next()

    if (current >= items.length)
        current = 0;

I added a new function loadItem() into the function, because this is where I want to handle adding the image/text to the slideshow. And I'll do the same for the previous function.

function previous()
        if (current < 0)
            current = images.length -1;

Just as I had hoped, it was that easy. current will be the index of the items array that is currently showing in the slideshow, and the loading of the item will pretty much just be grabbing that index out of the array, checking its type, and rendering it accordingly.

function loadItem()
	var parent = document.getElementById("content");
	parent.innerHTML = "";
	var item = items[current];

	if (item.type == "text")
		parent.innerHTML =;		

		var img = document.createElement("img");
		img.src =;

loadItem will grab the current item from the items array, check its type property, and render it correctly based on that. If I wanted too, I could have it handle links and html as well, which would be pretty useful. But that will be an exercise for another time.

Here is a full working version, so you can see it in action.

Leave a comment

No messages posted yet

Developer Poll


Add a comment

Send me your weekly newsletter filled with awesome ideas