How do we make basic image slideshow in JavaScript?

Basic image slideshow

As you know, a slideshow is made up of several images and videos changing over time. So how do you create a simple image slideshow using Javascript?

First, we enter our IMG tag in the HTML section.

Then we give it name , width and height . These values ​​may change depending on your wishes. Now, we select the images to use in the slideshow according to the dimensions we have specified in the img tag. If you have difficulty finding a picture, you can find pictures of the desired size on the Internet. You can use the JavaScript section by copying the address of the images from those pages.

<h1>How do we make basic image slideshow in JavaScript?</h1><img name = "slide" width="600px" height="550px">

After completing our small code on the Html side, let’s move on to the main part, JavaScript. First of all, let’s define a variable that will change our images constantly:

var i = 0;

Next, we define a variable to be able to use the images whose addresses we copied:

var images = ['' ,'' ,'' ,'' ,];

As we said at the beginning, a slideshow is made up of images changing over time. Now we have to set that time interval:

var timeDuration = 2000; //(1000 = 1 second)

Now we create a function to gather all our variables together and do the main work.

function slideFunction () {}

The first thing we will do is associate the images variable in the HTML section with the img tag in the JavaScript section. To do this:

document.slide.src = images [i];

Now we need to write code that will allow our images to rotate consistently.

if ( i < images.length - 1) {i++;}else {i = 0;}

While looking at this code, you may ask why i < images.length — 1?

Let’s discuss this after completing our code.

Now let’s apply the time duration to our function:

setTimeout( "slideFunction()", timeDuration);

Pictures will now appear in the time interval we applied, and after that interval, pictures will move to another picture.

Now we need to make sure that the codes we write are visible on our website. To do this:

window.onload = slideFunction();

With the help of this code, all the codes we write within the slideFunction function will start working after the web page is fully loaded.

Let’s put together all the codes we wrote:

var i = 0;var images = ['' ,'' ,'' ,'' ,];var timeDuration = 2000;function  slideFunction() {document.slide.src = images[i];if ( i < images.length - 1) {i++;}else {i = 0;}setTimeout( "slideFunction()", timeDuration);}window.onload = slideFunction();

In the end, if we run our code, we will get the same project as in the video.

Now let’s look at the question we faced above. Why not i < images.length , i<images.length-1 ?
To understand this clearly, let’s examine and compare both cases.

i < images.length-1
i < images.length

As you can see in the difference, when we use i <images.length, it eventually returns an empty value. It is advisable to use i <images.length-1, as this distorts the slide image.




Full-Stack developer. Founder of The-Black. Editor of Star Gazers publication. BEU

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Angular vs. React vs. Vue: Which Framework is Better?

Which Framework is Better? Blog cover image

Build a scalable front-end with Rush monorepo and React — Part 1

Changing Text Color based on the Background in JavaScript

Mobile-Friendly Table

Demystifying “==” and “===” in JavaScript

Need Closure?

JS Pipeline Operator |> Playground

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


Full-Stack developer. Founder of The-Black. Editor of Star Gazers publication. BEU

More from Medium

Array Iteration Methods: .map()

Array sort( ) method and comparator in Javascript

Recursive JavaScript method to process multiple directories drops in a Drag-n-drop upload operation

Converting Javascript Date objects to readable formats