Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

image gallery functions

const images = document.querySelectorAll(".gallery__item img");
let imgIndex
let imgSrc;
// get images src onclick
images.forEach((img, i) => {
    img.addEventListener("click", (e) => {
        imgSrc = e.target.src;
        //run modal function
        imgModal(imgSrc);
        //index of the next image
        imgIndex = i;
    });
});
//creating the modal
let imgModal = (src) => {
    const modal = document.createElement("div");
    modal.setAttribute("class", "modal");
    //add modal to the parent element 
    document.querySelector(".main").append(modal);
    //adding image to modal
    const newImage = document.createElement("img");
    newImage.setAttribute("src", src);
    //creating the close button
    const closeBtn = document.createElement("i");
    closeBtn.setAttribute("class", "fas fa-times closeBtn");
    //close function
    closeBtn.onclick = () => {
        modal.remove();
    };
//next and previous buttons
const nextBtn = document.createElement("i");
nextBtn.setAttribute("class", "fas fa-angle-right nextBtn");
// change the src of current image to the src of next image
nextBtn.onclick = () => {
    newImage.setAttribute("src", nextImg())
};
const prevBtn = document.createElement("i");
prevBtn.setAttribute("class", "fas fa-angle-left prevBtn");
// change the src of current image to the src of pevious image
prevBtn.onclick = () => {
    newImage.setAttribute("src", prevImg())
}
modal.append(newImage, closeBtn, nextBtn, prevBtn);
};
//next image function
let nextImg = () => {
    imgIndex++;
    //check if it is the the last image
    if (imgIndex >= images.length) {
        imgIndex = 0
    }
    //return src of the new image
    return images[imgIndex].src;
};
//previous image function
let prevImg = () => {
    imgIndex--;
    console.log(imgIndex);
    //check if it is the first image
    if (imgIndex < 0) {
        imgIndex = images.length - 1
    }
    //return src of previous image
    return images[imgIndex].src
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: angular universal prerender 
Javascript :: javascript merge modification in objects 
Javascript :: json format in .net core 
Javascript :: laravel onkeyup textbox, get value from span javascript 
Javascript :: React Router rendering blank pages for all components 
Javascript :: React.js setState on page load not working, how to fix 
Javascript :: hide navbar and footer on certain pages like dashboard. react-router 
Javascript :: google chrome extension v3 react content security policy issue 
Javascript :: Why is the return function in my debounce function never called? Angularjs 
Javascript :: angularjs No alignment and missing item in a vertical menu 
Javascript :: directive with ng-if not rendering in Angular.js 
Javascript :: Angularjs - Deep Orderby - How to handle multiple layers of sorting 
Javascript :: AngularJS Form validation transition to valid when some elements are not even touched yet 
Javascript :: React Native Root Element, deciding on async call 
Javascript :: In React Native / Expo, is there any way to save a specific part of an image 
Javascript :: adding to an array in js 
Javascript :: get the character code in a string 
Javascript :: Connect session middleware - regenerate vs reload 
Javascript :: convert json to string curl 
Javascript :: varibale as listeneres javascirpit 
Javascript :: how can do i open the select tag using keyboard event using javascript site:stackoverflow.com 
Javascript :: phaser set mass 
Javascript :: array loop pyramid js 
Javascript :: react router how to prevent navlink from two classes 
Javascript :: react console logs not working 
Javascript :: Declaring A Method Outside The Constructor 
Javascript :: check if can go back react native 
Javascript :: todo app html css javascript 
Javascript :: How to Loop Through an Array with a do…while Loop in JavaScript 
Javascript :: remove T from datetime in js 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =