An "Interactive Image Gallery with Previous and Next Navigation in JavaScript" is a web component that allows users to view a collection of images in a visually engaging and user-friendly manner. The gallery provides buttons or controls to navigate between images, usually labeled as "Previous" and "Next," enabling users to cycle through the images one by one
Here's a step-by-step explanation of how it works:
Here is an example code snippet that demonstrates this approach:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <link rel="stylesheet" href="css/style.css" /> </head> <body> <div class="container"> <div class="images"> <div> <span>Image Caption-1</span> <img src="images/1.jpg" alt="Image Caption-1" /> </div> <div> <span>Image Caption-2</span> <img src="images/2.jpg" alt="Image Caption-2" /> </div> <div> <span>Image Caption-3</span> <img src="images/3.jpg" alt="Image Caption-3" /> </div> <div> <span>Image Caption-4</span> <img src="images/4.jpg" alt="Image Caption-4" /> </div> <div> <span>Image Caption-5</span> <img src="images/5.jpg" alt="Image Caption-5" /> </div> <div> <span>Image Caption-6</span> <img src="images/6.jpg" alt="Image Caption-6" /> </div> <div> <span>Image Caption-7</span> <img src="images/7.jpg" alt="Image Caption-7" /> </div> <div> <span>Image Caption-8</span> <img src="images/8.jpg" alt="Image Caption-8" /> </div> <div> <span>Image Caption-9</span> <img src="images/9.jpg" alt="Image Caption-9" /> </div> <div> <span>Image Caption-10</span> <img src="images/10.jpg" alt="Image Caption-10" /> </div> <div> <span>Image Caption-11</span> <img src="images/11.jpg" alt="Image Caption-11" /> </div> <div> <span>Image Caption-12</span> <img src="images/12.jpg" alt="Image Caption-12" /> </div> </div> </div> <div class="modal"> <span class="close">×</span> <div class="modal-content"> <img src="images/2.jpg" alt="" class="modal-img" /> <div class="modal-txt">Sample Text</div> <div class="modal-nav"> <button type="button" class="btnPrev">❮</button> <button type="button" class="btnNext">❯</button> </div> </div> </div> <script src="js/script.js"></script> </body> </html>
document.addEventListener("DOMContentLoaded", function () { const images = document.querySelectorAll(".images img"); const modal = document.querySelector(".modal"); const modalImg = document.querySelector(".modal-img"); const modalTxt = document.querySelector(".modal-txt"); const closebtn = document.querySelector(".close"); let currentIndex = 0; const prevBtn = document.querySelector(".btnPrev"); const nextBtn = document.querySelector(".btnNext"); //Add Click Event for All Images images.forEach((image, index) => { image.addEventListener("click", function () { currentIndex = index; updateModalContent(); modal.classList.add("show"); }); }); //Update Image in Modal function updateModalContent() { const image = images[currentIndex]; modalImg.classList.remove("show"); setTimeout(() => { modalImg.src = image.src; modalTxt.innerHTML = image.alt; modalImg.classList.add("show"); }, 300); } //Next button onclick nextBtn.addEventListener("click", function () { currentIndex = currentIndex + 1 >= images.length ? 0 : currentIndex + 1; updateModalContent(); }); //Previous button onclick prevBtn.addEventListener("click", function () { currentIndex = currentIndex - 1 < 0 ? images.length - 1 : currentIndex - 1; updateModalContent(); }); //Code for Close Icon closebtn.addEventListener("click", function () { modal.classList.remove("show"); }); });
This code is a JavaScript implementation of an interactive image gallery with previous and next navigation, along with a modal popup for displaying images in a larger view. Let's go through the code step-by-step:
Overall, this JavaScript code creates an interactive image gallery with previous and next navigation, and it displays the selected image in a modal popup when clicked.
@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600&display=swap"); * { margin: 0; padding: 0; box-sizing: border-box; font-family: "Poppins", sans-serif; } body { width: 100vw; height: 100vh; display: grid; place-items: center; background-color: #ccc; } .container { padding: 10px; width: 900px; min-height: 800px; background-color: #f1f1f1; cursor: pointer; } .images { display: grid; grid-template-columns: repeat(3, 1fr); grid-gap: 1em; } .images img { width: 100%; height: 100%; object-fit: cover; } .images div { position: relative; } .images div span { position: absolute; left: 0; bottom: 0; width: 100%; background-color: rgba(0, 0, 0, 0.6); color: #fff; padding: 5px 10px; font-size: 12px; } .modal { position: fixed; height: 100%; width: 100%; top: 0; left: 0; background-color: rgba(0, 0, 0, 0.7); backdrop-filter: blur(2px); display: none; place-items: center; color: #fff; } .close { position: absolute; top: 1em; right: 1.5em; font-size: 25px; cursor: pointer; } .modal-content { min-height: 800px; width: 800px; display: flex; flex-direction: column; justify-content: center; } .modal-content img { width: 100%; object-fit: cover; opacity: 0; transition: opacity 0.5s ease-in-out; } .modal-content img.show { opacity: 1; } .modal-txt { font-size: 20px; padding: 10px 5px; } .modal-nav { padding: 10px 5px; } .modal-nav button { padding: 5px; width: 100px; font-size: 20px; background-color: transparent; color: #fff; border: 1px solid #ccc; border-radius: 2px; cursor: pointer; transition: 0.5s; } .modal-nav button:hover { background-color: teal; } .show { display: grid; }
By combining HTML, CSS, and JavaScript, an interactive image gallery with previous and next navigation provides a visually appealing and engaging way for users to explore a collection of images on a website.
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions