Skip to main content

JavaScript - Image Gallery Source Code

Language


In this tutorial we will create a Image Gallery Source Code using Javascript. This code will organize your image display into a gallery layout in order to make it easier to view. The code use onclick() function to call a specific method that will change the main image display when the sub images is been clicked by the use of event. This is a free sourcecode please use it and modify it as you want.

We will be using JavaScript as a server-side scripting language because It gives a greater control of your web page and extend its capability in a modern way approach. It is written in HTML or as an external sourcing to add some necessary features in your website.

Getting started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design 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:123px;
  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 - Image Gallery Source Code</h3>
  23. <hr style="border-top:1px dotted #ccc;">
  24. <center><img height="300px" width="100%" style="border:1px solid #000;" src="images/image 1.jpg" id="feature"/></center>
  25. <br /><br />
  26. <div id="gallery" onclick="selectedImage(event)">
  27. <img class="subImage" src="images/image 1.jpg" />
  28. <img class="subImage" src="images/image 2.jpg" />
  29. <img class="subImage" src="images/image 3.jpg" />
  30. <img class="subImage" src="images/image 4.jpg" />
  31. <img class="subImage" src="images/image 5.jpg" />
  32. </div>
  33. </div>
  34.  
  35. </body>
  36. <script src="js/script.js"></script>
  37. </html>

Creating the Script

This code contains the script of the application. This code will change the main image value when the sub images is clicked. 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. function selectedImage(e){
  4. e = event || window.event;
  5.  
  6. var target = e.target || e.srcElement;
  7.  
  8. if(target.tagName == "IMG"){
  9. document.getElementById("feature").src = target.getAttribute("src");
  10.  
  11. }
  12. }
  13.  
  14. for(var i=0; i < images.length; i++){
  15.  
  16. images[i].onmouseover = function(){
  17. this.style.cursor = 'hands';
  18. this.style.borderColor = '#1a1aff';
  19. }
  20.  
  21. images[i].onmouseout = function(){
  22. this.style.cursor = 'pointer';
  23. this.style.borderColor = 'grey';
  24. }
  25. }

There you have it we successfully created a Image Gallery Source Code 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

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.