Skip to content Skip to sidebar Skip to footer

How To Display Two Slideshows On The Same Page?

I'm using this slideshow as an example, which works perfectly fine. There are shared classes between each slideshow which means that when one finishes only then can the next one be

Solution 1:

I dont know if I should do this... But here goes Object Oriented Style of doing things...

  1. Create single Class for ALL SLIDES
  2. Create instances of each slide
  3. Call functions for the instance you want.. The thing I edited in your html was the onclick calls for the Instances(or objects) and I wrapped both slides and dots in a single section div..

Now you have two sections of slides( slide + dots ) working independently..

let slide1 = newCreateSlide(0);
let slide2 = newCreateSlide(1);

functionCreateSlide(index) {
    this.slideContainer = document.getElementsByClassName("section")[index];
    this.slideIndex = 1;

    this.plusSlides = function(n) {
        this.showSlides(this.slideIndex += n);
    };
    this.currentSlide = function(n) {
        this.showSlides(this.slideIndex = n);
    };
    this.showSlides = function(n) {
        var i;
        var slides = this.slideContainer.getElementsByClassName("mySlides");
        var dots = this.slideContainer.getElementsByClassName("dot");
        if (n > slides.length) {
            this.slideIndex = 1
        }
        if (n < 1) {
            this.slideIndex = slides.length
        }
        for (i = 0; i < slides.length; i++) {
            slides[i].style.display = "none";
        }
        for (i = 0; i < dots.length; i++) {
            dots[i].className = dots[i].className.replace(" active", "");
        }
        slides[this.slideIndex - 1].style.display = "block";
        dots[this.slideIndex - 1].className += " active";
    }
    this.showSlides(1);
}
* {
    box-sizing: border-box
}
body {
    font-family: Verdana, sans-serif;
    margin: 0
}
/* Slideshow container */.slideshow-container {
    position: relative;
    background: #f1f1f1f1;
 }
 /* Slides */.mySlides {
     display: none;
     padding: 80px;
     text-align: center;
 }
 /* Next & previous buttons */.prev, .next {
     cursor: pointer;
     position: absolute;
     top: 50%;
     width: auto;
     margin-top: -30px;
     padding: 16px;
     color: #888;
     font-weight: bold;
     font-size: 20px;
     border-radius: 03px3px0;
     user-select: none;
 }
 /* Position the "next button" to the right */.next {
     position: absolute;
     right: 0;
     border-radius: 3px003px;
 }
 /* On hover, add a black background color with a little bit see-through */.prev:hover, .next:hover {
     background-color: rgba(0, 0, 0, 0.8);
     color: white;
  }
  /* The dot/bullet/indicator container */.dot-container {
      text-align: center;
      padding: 20px;
      background: #ddd;
  }
  /* The dots/bullets/indicators */.dot {
      cursor: pointer;
      height: 15px;
      width: 15px;
      margin: 02px;
      background-color: #bbb;
      border-radius: 50%;
      display: inline-block;
      transition: background-color 0.6s ease;
  }
  /* Add a background color to the active dot/circle */.active, .dot:hover {
      background-color: #717171;
  }
  /* Add an italic font style to all quotes */q {
      font-style: italic;
  }
  /* Add a blue color to the author */.author {
      color: cornflowerblue;
  }
<h1>Slideshow 1</h1><divclass="section"><divclass="slideshow-container"><divclass="mySlides"><q>I love you the more in that I believe you had liked me for my own sake and for nothing else</q><pclass="author">- John Keats</p></div><divclass="mySlides"><q>But man is not made for defeat. A man can be destroyed but not defeated.</q><pclass="author">- Ernest Hemingway</p></div><divclass="mySlides"><q>I have not failed. I've just found 10,000 ways that won't work.</q><pclass="author">- Thomas A. Edison</p></div><aclass="prev"onclick="slide1.plusSlides(-1)"></a><aclass="next"onclick="slide1.plusSlides(1)"></a></div><divclass="dot-container"><spanclass="dot"onclick="slide1.currentSlide(1)"></span><spanclass="dot"onclick="slide1.currentSlide(2)"></span><spanclass="dot"onclick="slide1.currentSlide(3)"></span></div></div><divclass="section"><h1>Slideshow 2</h1><divclass="slideshow-container"><divclass="mySlides"><q>I love you the more in that I believe you had liked me for my own sake and for nothing else</q><pclass="author">- John Keats</p></div><divclass="mySlides"><q>But man is not made for defeat. A man can be destroyed but not defeated.</q><pclass="author">- Ernest Hemingway</p></div><divclass="mySlides"><q>I have not failed. I've just found 10,000 ways that won't work.</q><pclass="author">- Thomas A. Edison</p></div><aclass="prev"onclick="slide2.plusSlides(-1)"></a><aclass="next"onclick="slide2.plusSlides(1)"></a></div><divclass="dot-container"><spanclass="dot"onclick="slide2.currentSlide(1)"></span><spanclass="dot"onclick="slide2.currentSlide(2)"></span><spanclass="dot"onclick="slide2.currentSlide(3)"></span></div></div>

Post a Comment for "How To Display Two Slideshows On The Same Page?"