JavaScript - Simple Image Gallery
Submitted by razormist on Monday, July 2, 2018 - 20:54.
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...
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!!
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.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
- <style type="text/css">
- .subImage{
- height:100px;
- width:100px;
- border: 1px solid #000;
- }
- </style>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- </div>
- </nav>
- <div class="col-md-6 well">
- <hr style="border-top:1px dotted #ccc;">
- <img height="500px" width="500px" style="border:1px solid #000;" class="pull-left" src="images/image 1.jpg" id="feature"/>
- <div id="gallery" onclick="ChangeImage(event)">
- <img class="subImage" src="images/image 1.jpg" />
- <img class="subImage" src="images/image 2.png" />
- <img class="subImage" src="images/image 3.png" />
- <img class="subImage" src="images/image 4.png" />
- <img class="subImage" src="images/image 5.png" />
- </div>
- </div>
- </body>
- </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.- var images = document.getElementById("gallery").getElementsByTagName("img");
- for(var i=0; i < images.length; i++){
- images[i].onmouseover = function(){
- this.style.cursor = 'hands';
- this.style.borderColor = '#1a1aff';
- }
- images[i].onmouseout = function(){
- this.style.cursor = 'pointer';
- this.style.borderColor = 'grey';
- }
- }
- function ChangeImage(e){
- e = event || window.event;
- var target = e.target || e.srcElement;
- if(target.tagName == "IMG"){
- document.getElementById("feature").src = target.getAttribute("src");
- }
- }
Add new comment
- 214 views