JavaScript - Simple Image Gallery

In this tutorial we will create a Simple Image Gallery using Javascript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is a text-based programming language meant to run as part of a web-based application. 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 https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. 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.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6.                 <style type="text/css">
  7.                         .subImage{
  8.                                 height:100px;
  9.                                 width:100px;
  10.                                 border: 1px solid #000;
  11.                         }
  12.                 </style>
  13.         </head>
  14.         <nav class="navbar navbar-default">
  15.                 <div class="container-fluid">
  16.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester.com</a>
  17.                 </div>
  18.         </nav>
  19.        
  20.         <div class="col-md-3"></div>
  21.         <div class="col-md-6 well">
  22.                 <h3 class="text-primary">Javascript - Simple Image Gallery</h3>
  23.                 <hr style="border-top:1px dotted #ccc;">
  24.                 <img height="500px" width="500px" style="border:1px solid #000;"  class="pull-left" src="images/image 1.jpg" id="feature"/>
  25.                 <div id="gallery" onclick="ChangeImage(event)">
  26.                         <img class="subImage" src="images/image 1.jpg" />
  27.                         <img class="subImage" src="images/image 2.png" />
  28.                         <img class="subImage" src="images/image 3.png" />
  29.                         <img class="subImage" src="images/image 4.png" />
  30.                         <img class="subImage" src="images/image 5.png" />
  31.                 </div>
  32.         </div>
  33.        
  34. </body>
  35. <script type="text/javascript"></script>
  36.  
  37. </html>

Creating the Script

This code contains the script of the application. This code will get the image value source, when the image div is clicked. It will toggle to each image source base on the target. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js folder.
  1. var images = document.getElementById("gallery").getElementsByTagName("img");
  2.                
  3.         for(var i=0; i < images.length; i++){
  4.                
  5.                 images[i].onmouseover = function(){
  6.                         this.style.cursor = 'hands';
  7.                         this.style.borderColor = '#1a1aff';
  8.                 }
  9.                
  10.                 images[i].onmouseout = function(){
  11.                         this.style.cursor = 'pointer';
  12.                         this.style.borderColor = 'grey';
  13.                 }
  14.         }
  15.  
  16.        
  17.        
  18.         function ChangeImage(e){
  19.                 e = event || window.event;
  20.                
  21.                 var target = e.target || e.srcElement;
  22.                        
  23.                 if(target.tagName == "IMG"){
  24.                         document.getElementById("feature").src = target.getAttribute("src");
  25.                        
  26.         }
  27. }
There you have it we successfully created a Simple Image Gallery 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