How to Make a Slideshow Gallery in JavaScript

How to Make a Slideshow Gallery in JavaScript

Introduction

In this tutorial we will create a How to Make a Slideshow Gallery in JavaScript. This tutorial purpose is to teach how to create a slideshow for your website. This will tackle the randomization of images that will be shown. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is very easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to your website that needed for displaying your images. I will try my best to give you the easiest way of creating this program Slideshow Gallery. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will display the image and buttons. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  5.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6.         </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">How to Make a Slideshow Gallery</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-8">
  17.                         <img src="images/img1.png" name="slideshow" height="300px" width="400px"/>
  18.                 </div>
  19.                 <div class="col-md-4">
  20.                         <div class="btn-group">
  21.                                 <button onclick="SetAuto()" class="btn btn-success"><span class="glyphicon glyphicon-repeat"></span></button>
  22.                                 <button  onclick="ChangeImage(-1)"  class="btn btn-primary"><span class="glyphicon glyphicon-arrow-left"></span></button>
  23.                                 <button onclick="ChangeImage(1)" class="btn btn-primary"><span class="glyphicon glyphicon-arrow-right"></span></button>
  24.                         </div>
  25.                 </div>
  26.                
  27.                        
  28.                
  29.         </div>
  30. </body>
  31. <script src="script.js"></script>
  32. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will play the image slideshow automatically or manually. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. var image = new Array("images/img1.png", "images/img2.jpg", "images/img3.jpg", "images/img4.jpg");
  2. var count = 0;
  3. var length = image.length - 1;
  4.  
  5. function ChangeImage(num){
  6.        
  7.         count = count + num;
  8.  
  9.         if(count > length){
  10.                 count = 0;
  11.         }
  12.        
  13.         if(count < 0){
  14.                 count = length;
  15.         }
  16.        
  17.         document.slideshow.src = image[count];
  18.        
  19.         return false;
  20. }
  21.  
  22. function SetAuto(){
  23.         setInterval("ChangeImage(1)", 4000);
  24. }

In this code we first set all the variable and name them accordingly image, count, length. Then we create a method called ChangeImage(), this will handle the changing of images manually. Lastly we create a method that will automate the image slideshow by setting it with an interval and we then call it SetAuto().

Output:

The How to Make a Slideshow Gallery in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Make a Slideshow Gallery in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment