Skip to main content

JavaScript - Image Gallery Source Code

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

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=""></a>
  17. </div>
  18. </nav>
  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>
  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");
  3. function selectedImage(e){
  4. e = event || window.event;
  6. var target = || e.srcElement;
  8. if(target.tagName == "IMG"){
  9. document.getElementById("feature").src = target.getAttribute("src");
  11. }
  12. }
  14. for(var i=0; i < images.length; i++){
  16. images[i].onmouseover = function(){
  17. = 'hands';
  18. = '#1a1aff';
  19. }
  21. images[i].onmouseout = function(){
  22. = 'pointer';
  23. = '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