简体   繁体   中英

Adding more than one javascript image slider on same page

I want more than one image slider on same page with same class name using JavaScript. I have done for one image slider.

The issues are when I'm using more than one image slider it is not working properly.I tried with childNodes also it doesn't work. How can I solve this problem?

And I'm trying to make sliding animation also(left and right) for that image slider.

If you need some more explanation, let me know.

I have given the code below;

HTML

<div class="container slider">
    <div class="slides">
        <img src="images-/b3.jpg">
    </div>
    <div class="slides">
        <img src="images-/b2.jpg">
    </div>
    <div class="slides">
        <img src="images-/b1.jpg">
    </div>
    <a class="prev" onclick="controlSlide(-1)">&#10094;</a>
    <a class="next" onclick="controlSlide(1)">&#10095;</a> 
</div>  

<div class="container slider">
    <div class="slides">
        <img src="images-/b3.jpg">
    </div>
    <div class="slides">
        <img src="images-/b2.jpg">
    </div>
    <div class="slides">
        <img src="images-/b1.jpg">
    </div>
    <a class="prev" onclick="controlSlide(-1)">&#10094;</a>
    <a class="next" onclick="controlSlide(1)">&#10095;</a> 
 </div>

CSS

.slides{
  position: relative;
  display:none;
}
img{
  width: 100%;
  vertical-align:middle;
}
.container  
{ 
  max-width: 100%; 
  position: relative; 
  margin: auto; 
}
.prev, .next {
  cursor: pointer;
  position: absolute;
  top: 50%;
  color: black;
  font-size: 20px;
} 
.prev{
  left: 2%;
  } 
.next{
  right: 2%;
}
.prev:hover, .next:hover {
  background-color: #f1f1f1;
  text-decoration: none;
  color: black;
}

JAVASCRIPT

var slides = document.getElementsByClassName("slides");
var position=[1,-1];
var slideIndex=0;
showSlides();

function showSlides() { 
    controlSlide(position[0]);
    setTimeout(showSlides,3000);
} 

function controlSlide(position) {
    for (i = 0; i < slides.length; i++) {
       slides[i].style.display = "none";  
    }
    slideIndex +=position;
    if (slideIndex>slides.length) {
        slideIndex = 1; 
    }
    else if(slideIndex<=0){
        slideIndex=slides.length; 
    }
    slides[slideIndex-1].style.display= "block";
}

The problem is that you select all the slides at once, with document.getElementsByClassName("slides").

You want to alter you JS methods to work with the sorrounding element

<div class="container slider">

and then select and pass theese to controlSlide form showSlides. Not tested:

var sliders = document.getElementsByClassName("slider");
var position=[1,-1];
var slideIndex=0;

sliders.forEach(function(slider){
    showSlides(slider);
})


function showSlides(slider) {
    let slides = slider.childNodes; 
    controlSlide(slides, position[0]);
    setTimeout(showSlides,3000);
} 

function controlSlide(slides, position) {
    for (i = 0; i < slides.length; i++) {
       slides[i].style.display = "none";  
    }
    slideIndex +=position;
    if (slideIndex>slides.length) {
        slideIndex = 1; 
    }
    else if(slideIndex<=0){
        slideIndex=slides.length; 
    }
    slides[slideIndex-1].style.display= "block";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM