Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

multiple images gallery after clicking image javascript

const data=[["https://dummyimage.com/100x100/ad40ad/000000","https://dummyimage.com/100x100/454545/000000","https://dummyimage.com/100x100/789acc/000000","https://dummyimage.com/100x100/994563/000000","https://dummyimage.com/100x100/fff456/000000"],["https://dummyimage.com/100x100/ad40ad/000000","https://dummyimage.com/100x100/454545/000000","https://dummyimage.com/100x100/789acc/000000","https://dummyimage.com/100x100/994563/000000","https://dummyimage.com/100x100/fff456/000000"]];

// Returns a new array of images from
// the source array
function loadImageSet(arr) {
  return new Promise(res => {
    const set = arr.map(src => {
      const img = new Image();
      img.src = src;
      return img;
    });
    res(set);
  });
}

// Returns a nested array of images
function preload(data) {
  return Promise.all(data.map(loadImageSet));
}

async function main(data) {
  
  // Get the images
  const images = await preload(data);

  // Cache the containers
  const containers = document.querySelectorAll('div');

  // For each container add a listener. `handleClick`
  // does some initial setup (using the id, and image set),
  // and then returns a new function that is called
  // when `click` is triggered
  containers.forEach(c => {
    const { id } = c.dataset;
    const imageSet = images[id - 1];
    c.addEventListener('click', handleClick(c, id, imageSet), false);
  });

};

// Call the main function
main(data);

// Accepts an id and an image set
function handleClick(container, id, imageSet) {

  // Initialises the array index
  let index = 0;

  // Initialises the first image
  container.appendChild(imageSet[index]);
  
  // Increases the index
  ++index;

  // Return the function that is called when
  // the click event is triggered
  return function () {

    // Reset `index` if it hits the `imageSet` length
    if (index === imageSet.length) index = 0;

    // Replace the image with the next one in
    // in the `imageSet` array
    const img = container.querySelector('img');
    img.replaceWith(imageSet[index]);

    // Finally increase `index`
    ++index;
  }

}
Comment

PREVIOUS NEXT
Code Example
Javascript :: Backbone Model Vs Backbone Collection 
Javascript :: Backbone.model first parameter determines properties that each instance must have 
Javascript :: js extend list 
Javascript :: removing the first match in array 
Javascript :: _.extend can be used to attach functions to a prototype like this 
Javascript :: Will yield function Person 
Javascript :: add a cumma in a number jquery 
Javascript :: Update array with new object JavaScript without using index 
Javascript :: how to Play/start or pause timer in javascript 
Javascript :: Backbone Models In Collection Is Added Here 
Javascript :: santance case in javascript 
Javascript :: react axios project importing online same products with table from fake API 
Javascript :: modify summernote with js 
Javascript :: Listen to custom event in Vue.js 
Javascript :: populate strapi v4 
Javascript :: Just allow Intergers in Input Field 
Javascript :: math min js 
Javascript :: convert bytes to kb or mb javascript 
Javascript :: prisma.db json 
Javascript :: javascript interview questions geeksforgeeks 
Javascript :: how to square number in javascript 
Javascript :: react native extract cookie from response 
Javascript :: sentry configure scope 
Javascript :: tempusdominus calendar out of background size 
Javascript :: jquery keypress div color change 
Javascript :: Plumsail To change the modal popup window size you can try injecting the CSS to the SharePoint list view page 
Javascript :: getderivedfromstate alternative 
Javascript :: React Router rendering blank pages for all components 
Javascript :: angularjs Why does using .match inside of an ng-if seem to cause digest cycles 
Javascript :: AngularJs - Display Validation Message within Component 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =