Slideshow in JavaScript
Posted by Superadmin on May 02 2023 14:18:46

Slideshow in JavaScript 

Slideshow in JavaScript 

Introduction to Slideshow in JavaScript

Slideshow is nothing but showing images one after another after specific duration say 1 second. You might have already seen your photos in your computer in a slideshow. Correct! This is the same. In this tutorial, we are going to implement slideshow using HTML and JavaScript. HTML (Hyper Text Markup Language) is a markup language used to create web pages. This is the base language of every high-level web-based technologies. There are tags, used to design web pages.

Ex –

<h1>Heading</h1>

h1 tag is used to show heading of the web pages.

Second most important language in web development is JavaScript. Any business logic at client side is written in JavaScript and used in HTML pages in “<script>” tag. JavaScript is the default scripting language of HTML.

This was a short introduction to HTML and JavaScript.

Example of Slideshow in JavaScript

Now let’s implement the program for slideshow using these languages.

MySlideShow.html

Code:

<html>
<head>
<script src="MySlideShow.js"> </script>
<title>Slideshow using JavaScript</title>
</head>
<body>
<button onclick="stop_slideshow()">Stop</button>
<button onclick="start_slideshow()">Start</button>
<img class="slideshow_images" src="green_grass.jpg">
<img class="slideshow_images" src="nature.jpg">
<img class="slideshow_images" src="Waterfall.jpg">
<img class="slideshow_images" src="sunrise_in_jungle.jpg">
<script>
slideshow("slideshow_images", 1500);
</script>
</body>
</html>

MySlideShow.js

Code:

var myVar;
var myClassName;
var myInterval;
function slideshow(className, interval)
{
myClassName = className;
myInterval = interval;
console.log("Function executed with class name : " + className);
var allImages = document.getElementsByClassName(className);
console.log("Number of images : " + allImages.length);
for (var i=0; i<allImages.length; i++){
console.log("Image : " + i);
allImages[i].style.display = "none";
allImages[i].style.width = "50%";
allImages[i].style.marginLeft = "25%";
allImages[i].style.marginTop = "100px";
}
var j=-1;
myVar = setInterval(
function (){
j++;
if(j == 0)
{
allImages[allImages.length-1].style.display = "none";
}
if(j > 0){
allImages[j-1].style.display = "none";
}
console.log("Image : " + j);
allImages[j].style.display = "block";
if(j == allImages.length-1){
j = -1;
}
}, interval);
}
function start_slideshow(){
slideshow(myClassName, myInterval);
}
function stop_slideshow(){
clearInterval(myVar);
}

Explanation

 Technical Explanation

HTML Tags used in this Example

JavaScript functions used in this Application

Output

1) This is our application shown in browser. You can see on console that images are shown one after another.

slideshow javascript 1

2) When clicked on stop button, slideshow is stopped:

Example 2

3) When clicked on start button, slideshow is started again.slideshow javascript 3

Conclusion

JavaScript is very powerful client-side script which is default scripting language for HTML. We can use JavaScript for data validation and to implement any custom logic. Similarly, slideshow is implemented in JavaScript as shown in this tutorial.