How To Build an Image Slider From Scratch in HTML/CSS/JS

Ryan Wooff
9 min readDec 15, 2018

--

Want to build an image slider from scratch to use in any website?

In this tutorial you will learn how to build a simple website that displays a slideshow of images. This code isn’t perfect but I recently used this method to make a super fast slideshow without having to use anything more than a basic js file, and 56 high res images. This method loads one image at a time based on user input, and if you follow this tutorial, at the end you should have a fully functioning slideshow with super basic design, and the knowledge to implement it into your own website!

You can find all of the code on GitHub!

First things first, go ahead and setup a new folder. I’m gonna call mine “img-slider,” but you can call yours whatever you want.

Next add 3 more folders to that one called “js”, “css”, and “img”

Go ahead and open that first folder in the text editor of your choice, I use Sublime Text, and make a new file called index.html at the root of your project

next create a “style.css” file in the css folder, and an “init.js” file in the js folder

Now, you should have a project that looks something like this:

So, lets go ahead and get some basic code into that “index.html” file:

Now, go ahead and add your images to the img folder, it doesn’t matter how many, what size, or what format, but the naming is very important.

For this to work you need to rename all the images like this:

Now, lets link one of those images and check that everything’s working so far!

add this to the body of your “index.html” file above the <script> links

Next, open that index.html file in your web browser and you should see something like this:

Success!

Now its time for the javascript, the first thing to do is setup an array containing the img names, since we want to start at 1 the first name will be “null”, the second: “img_1.jpeg” and so on. I will be using 4 random images from the internet that all happen to be JPEG’s but that isn’t necessary. If you didn’t rename your images as described earlier, please do so now.

So, in your “init.js” file setup a function that will handle the slider operation:

add this to your “init.js” file in the js folder

This function is going to get passed a value “n” as a parameter, this is how we will tell it whether to go the next slide, or previous slide using 1 or 0 respectively.

Basically, the function slide is going to find the image in the site using jQuery, and then modify its SRC to display the next image.

To do this we will need to first get the image name from the SRC and split it to get a number from that image name, then depending on user input and our position in the list we will either go forward or backward in the list(increasing or decreasing the number in the image name), then with our new number we will rebuild the new image’s filepath and insert it into the html code, changing the displayed image to the new one.

Let’s dive in!

To make things easier later we are going to store the image’s name using a variable like so:

add line 3 of this code to your own code in “js/init.js”

This new line of code is going to find the image, and get its image name from the filepath, it then will separate the “img_” from our image name leaving us with a number and extension that gets stored in the variable imgName. For the first image in a folder it would go from “img_1.jpeg” to “1.jpeg”

With that ready to go we can start writing the logic for this function. Since we will either be going to the prev image or the next one we will need an if/else statement here to test whether n is equal to 0 or 1. In our html code we will then have buttons with onclick functions corresponding to these values, but we’ll worry about that later.

For now, create a simple if/else statement to test the value of n:

First let’s build the logic for going to the previous slide, we need to make sure that if we’re on the first image, that we don’t try to go to img_0 since it doesn’t exist, so first we have to see if we’re on the first image. Add this to your code:

imgName is storing the value “1.jpeg” for our first image, so if we split that and target the ‘.’ we can get an array containing [“1”, “jpeg”]

This code is getting an integer value from the first element in our newly created array, which will grab a numerical value of 1. seeing as we’re on the first image we will fail this test and the program will move to the else portion.

Lets get to work on that part now. This is the whole brains of the operation here, where we actually change the current image to a new one. First we need to access the image in the DOM using the same code from earlier:

Once the program has grabbed this object, we are going to change it with our new data. Currently, in the html code what we’re grabbing above looks like this: “<img class=”slider-img” src=”img/img_1.jpeg”>”

The data stored in imgName would look like

this:[“<img class=”slider-img” src=”img/”, “1.jpeg”>”]

So, we want to replace the outerHTML of this image with an updated version of the string containing our new information. to go to the previous image, if we’re starting on 1, we would want to get to 4. In html we want our end result to look like this: “<img class=”slider-img” src=”img/img_4.jpeg”>”

To make that line of code we’re going to need the first element of the imgName variable for most of the html code, which we will combine with our new image’s name. The new image’s name will be pulled from the array we created earlier called imgNames. First we will get the numerical value of the current image’s postition in the list, in our case it’s 1. To go the previous slide we want to go to image 4, so we will add 3 to the index if we’re on image 1 and click on the “previous” button. The code for all of that will look like this:

imgName[0] + imgNames[parseInt(imgName[1].split(‘.’)[0]) + 3] + “\”>”;

This will get us the proper image.

Add this to your code:

$(‘div#slider.slider img.slider-img’)[0].outerHTML = imgName[0] + imgNames[parseInt(imgName[1].split(‘.’)[0]) + 3] + “\”>”;

This red arrow shows where it should go:

Now, copy that same line of code into the if portion:

Figure 1

If you remember from earlier, we are building the “previous slide” logic when starting on the first slide, that was difficult but you did it! Hooray! Now we need to build the “previous slide” logic for when you’re not on the first slide. Thats the code you just copied for a second time, depicted in Figure 1, and instead of adding 3(since were not at the first slide) all we want is to subtract 1. Towards the end of that line of code(circled in red above) you want to change it from + 3 to - 1 so that when you’re on img_2 it will go back to img_1.

Great! The hard part is over. Now we just need to copy that same logic into the else statement for when we want to go to the next slide, copy the contents of the blue area into the red area in your own code:

Figure 2

Now, since this part of our code handle the “Next Slide” logic we actually want the opposite effect as we have above. To achieve this, its very simple. Instead of testing to see if we are on the first slide, we need to test for the last slide, on Line 12 in Figure 2 you will see the code tests for equivalence to 4, if you have 56 images in your folder, that 4 should be a 56. Next, instead of SUBTRACTING one on Line 13(Fig. 2) you will want to ADD one, and instead of ADDING 3 on Line 15(Fig. 2) you will want to SUBTRACT 3.

You may be wondering where the “3” in my code comes from. That number needs to be equal to the total number of images in your folder minus 1. If you have 56 images, that number needs to be 55.

Alas, the nasty javascript hacking is complete. Time to add functionality to our HTML page with two simple buttons utilizing onClick Functions, like so:

Add this to youre “index.html” file

Now, refresh that browser window where you tested the image before, and you should get something like this

Success! You Just created an Image Slideshow with HTML and JS, BUT, before you go, why not add some simple css and clean things up a bit?

Let’s make this a bit less chaotic. Everytime we see ‘div#slider.slider img.slider-img’ we should be using a variable instead. Lets call it selector.

Add This:

This way, you can add it to other projects and alter the selector quickly
You should have this in your “init.js” file now

One more thing in that file is ugly, let’s take every instance of parseInt(imgName[1].split(‘.’)[0])out and put that into a variable called imgNum

Phew, that’s better! but…

Let’s take it one step further:

Create a new function called getPath() that will handle some of the repetition in the code.

You should have this now!

Now lets take care of some more ugly bits!

Change this code:

To look like this:

Nice!

Now you should have something like this here:

Hmm….one more thing!

We can make use of an inline if statement to make this even more readable!

Lines 7–11 become one, and lines 15–19 become one

Yay! All cleaned up here, lets do ONE more thing!

Open up that style.css file and add this code:

add to “css/style.css”

OR you can use your favorite framework to make this baby look beautiful.

Congrats! Your finished project should look roughly like this:

Hopefully you enjoyed this as much as I did!

--

--