Venom
Venom

Reputation: 3

how to show multiple slideshow on single page

i am making a slideshow using w3schools. but this code is unable to show multiple slideshows on single page. can anybody help in modifying this code to work on multiple slideshows. i want to show more than 2 slideshows.

var slideIndex = 1;
showDivs(slideIndex);

function plusDivs(n) {
  showDivs(slideIndex += n);
}

function currentDiv(n) {
  showDivs(slideIndex = n);
}

function showDivs(n) {
  var i;
  var x = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("demo");
  if (n > x.length) {slideIndex = 1}    
  if (n < 1) {slideIndex = x.length}
  for (i = 0; i < x.length; i++) {
     x[i].style.display = "none";  
  }
  for (i = 0; i < dots.length; i++) {
     dots[i].className = dots[i].className.replace(" w3-white", "");
  }
  x[slideIndex-1].style.display = "block";  
  dots[slideIndex-1].className += " w3-white";
}
<div class="w3-content w3-display-container" style="max-width:800px">
  <img class="mySlides" src="https://www.w3schools.com/w3css/img_mountains_wide.jpg" style="width:100%">
  <img class="mySlides" src="https://www.w3schools.com/w3css/img_nature_wide.jpg" style="width:100%">
  <img class="mySlides" src="https://www.w3schools.com/w3css/img_fjords_wide.jpg" style="width:100%">
  <div class="w3-center w3-container w3-section w3-large w3-text-white w3-display-bottommiddle" style="width:100%">
    <div class="w3-left w3-hover-text-khaki" onclick="plusDivs(-1)">&#10094;</div>
    <div class="w3-right w3-hover-text-khaki" onclick="plusDivs(1)">&#10095;</div>
    <span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(1)"></span>
    <span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(2)"></span>
    <span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(3)"></span>
  </div>
</div>

Upvotes: 0

Views: 296

Answers (2)

Dan Nagle
Dan Nagle

Reputation: 5425

Without adding additional classes or ids to the existing tag structure it can be achieved with the following JavaScript code.

// initialisation
function initialiseDataValues() {
  var next, prev, slides;
  // Locate the slideshow containers
  var containers = document.querySelectorAll('div.w3-content.w3-display-container');
  // Loop through the containers
  for (var ix = 0; ix < containers.length; ix++) {
    // Count the number of slides
    slides = containers[ix].getElementsByClassName('mySlides');
    // Add 'data-slides' property to the container
    containers[ix].dataset.slides = slides.length || 0;
    if (containers[ix].dataset.slides > 0) {
      // Add 'data-idx' property to the container
      containers[ix].dataset.idx = 1;
      // Add event handler for 'previous' slide click
      prev = containers[ix].getElementsByClassName('w3-left');
      if (prev) {
        prev[0].addEventListener('click', goPrevSlide);
      }
      // Add event handler for 'next' slide click
      next = containers[ix].getElementsByClassName('w3-right');
      if (next) {
        next[0].addEventListener('click', goNextSlide);
      }
      // Show the first slide in the container
      showSlide(containers[ix], 1);
    }
  }
}

function goToSlide(index) {
  // The container is two levels above this element
  var container = event.target.parentNode.parentNode;
  showSlide(container, index);
}

function goPrevSlide() {
  // The container is two levels above this element
  var container = event.target.parentNode.parentNode;
  showSlide(container, Number.parseInt(container.dataset.idx) - 1);
}

function goNextSlide() {
  // The container is two levels above this element
  var container = event.target.parentNode.parentNode;
  showSlide(container, Number.parseInt(container.dataset.idx) + 1);
}

function showSlide(container, n) {
  var slides = container.getElementsByClassName('mySlides'),
      dots = container.getElementsByClassName('demo'),
      slideIdx = 0,
      dotIdx = 0,
      slideToShow = 0;
  if (n > slides.length) {
    container.dataset.idx = 1;
  } else if (n < 1) {
    container.dataset.idx = slides.length;
  } else {
    container.dataset.idx = n;
  }
  for (slideIdx = 0; slideIdx < slides.length; slideIdx++) {
    slides[slideIdx].style.display = 'none';
  }
  for (dotIdx = 0; dotIdx < dots.length; dotIdx++) {
    dots[dotIdx].classList.remove('w3-white');
  }
  slideToShow = Number.parseInt(container.dataset.idx) - 1;
  slides[slideToShow].style.display = 'block';
  if (slideToShow < dots.length) {
    dots[slideToShow].classList.add('w3-white');
  }
}

initialiseDataValues();

The JavaScript code adds the 'Previous' and 'Next' click event handlers so there is no need for them in your HTML code. Each of the handler functions walks up the DOM tree to get to the slideshow container. If you change the HTML you will need to amend the code.

<div class="w3-content w3-display-container" style="max-width:800px">
  <img class="mySlides" src="https://www.w3schools.com/w3css/img_mountains_wide.jpg" style="width:100%">
  <img class="mySlides" src="https://www.w3schools.com/w3css/img_nature_wide.jpg" style="width:100%">
  <img class="mySlides" src="https://www.w3schools.com/w3css/img_fjords_wide.jpg" style="width:100%">
  <div class="w3-center w3-container w3-section w3-large w3-text-white w3-display-bottommiddle" style="width:100%">
    <div class="w3-left w3-hover-text-khaki">&#10094;</div>
    <div class="w3-right w3-hover-text-khaki">&#10095;</div>
    <span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="goToSlide(1)"></span>
    <span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="goToSlide(2)"></span>
    <span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="goToSlide(3)"></span>
  </div>
</div>

Upvotes: 1

radarbob
radarbob

Reputation: 5101

I'd do a two step process.

First, copy and paste the code. Change as needed to work for the second slide show. When that works do the next step.

Second, make a function that will work for any and all slideshows, removing the duplication of step one in the process. This function is simply the existing function but with everything that needed changing becomes parameters of the function. For example, every slide show ("mySlides") must have a different name.

Upvotes: 0

Related Questions