Javascript - Simple Slideshow

In this tutorial we will create a Simple Slideshow using Javascript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is widely used in designing a stunning website. It is an interpreted programming language that has a capabilities of Object-Oriented. So Let's do the coding.

Before we started:

This is the link for the bootstrap that has been used for the layout of the calculator https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. This code will display the image within the javascript function. To create this just write these block of code inside the text editor and save this 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">Javascript - Simple Slideshow</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <center><img src="images/pic1.jpeg" name="slideshow" height="400px" width="500px"></center>
  17. <button onclick="SetAuto()" class="btn btn-success"><span class="glyphicon glyphicon-repeat"></span> Auto</button>
  18. <button onclick="ChangeImage(-1)" class="btn btn-primary"><span class="glyphicon glyphicon-arrow-left"></span></button>
  19. <button onclick="ChangeImage(1)" class="btn btn-primary"><span class="glyphicon glyphicon-arrow-right"></span></button>
  20. </center>
  21. </div>
  22. </body>
  23. <script src="slideshow.js"></script>
  24. </html>

Creating the Script

This code contains the script of the application. This code will try to select different images from the source folder everytime the button is clicked. To do this just copy write the code as shown below inside the text editor and save it as slideshow.js.
  1. var image = new Array("images/pic1.jpeg", "images/pic2.jpeg", "images/pic3.jpg", "images/pic4.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. }
There you have it we successfully created a Simple Slideshow using 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!!!

Add new comment