JavaScript:如何使用相同的脚本在一个页面上添加多个图像滑块?

JavaScript:如何使用相同的脚本在一个页面上添加多个图像滑块?,javascript,html,Javascript,Html,我很难在一个页面上有多个滑块。我已经修复了一些随机问题,比如在stackoverflow上搜索时图像不显示或消失,但对于这个问题,我一直在寻找类似问题的答案,但我不太明白他们是如何修复的,因为只有固定代码被发布了 然而,据我所知,每个sliderdiv应该有一个不同的scripd,但我无法解决它 这是我的密码: HTML: 脚本: CSS: 首先,您需要向容器中添加id,并向函数中添加其他参数,以便它们知道哪些滑块需要更改: <div class="slideshow-container"

我很难在一个页面上有多个滑块。我已经修复了一些随机问题,比如在stackoverflow上搜索时图像不显示或消失,但对于这个问题,我一直在寻找类似问题的答案,但我不太明白他们是如何修复的,因为只有固定代码被发布了

然而,据我所知,每个sliderdiv应该有一个不同的scripd,但我无法解决它

这是我的密码:

HTML:

脚本:

CSS:


首先,您需要向容器中添加id,并向函数中添加其他参数,以便它们知道哪些滑块需要更改:

<div class="slideshow-container" id="first">

 <!-- Full-width images with number and caption text -->
     <div class="mySlides fade">
         <img src="/images/1.jpg" style="width:100%">
     </div>

     <div class="mySlides fade">
         <img src="/images/2.jpg" style="width:100%">
     </div>

     <div class="mySlides fade">
        <img src="/images/3.jpg">
     </div>

 <!-- Next and previous buttons -->
 <a class="prev" onclick="plusSlides(-1, 'first', 'firstSliderIndex')">&#10094;</a>
  <a class="next" onclick="plusSlides(1, 'first', 'firstSliderIndex')">&#10095;</a>
  <br />
  <!-- The dots/circles -->
<div style="text-align:center">
  <span class="dot" onclick="currentSlide(1, 'first', 'firstSliderIndex')"></span>
  <span class="dot" onclick="currentSlide(2, 'first', 'firstSliderIndex')"></span>
  <span class="dot" onclick="currentSlide(3, 'first', 'firstSliderIndex')"></span>
</div>
</div>
您的脚本:

<script>
    var indexes = {
        firstSliderIndex: 1,
        secondSliderIndex: 2,
    };
    showSlides(indexes.firstSliderIndex, 'first', 'firstSliderIndex');
    // Next/previous controls
    function plusSlides(n, id, index) { // n - number of slide, id - container id, index - current slide number in slider
        showSlides(indexes[index] += n, id, index);
    }
    // Thumbnail image controls
    function currentSlide(n, id, index) {
        showSlides(indexes[index] = n, id, index);
    }

    function showSlides(n, id, index) {
        var i;
        var slides = document.querySelectorAll(`#${id} .mySlides`);
        var dots = document.querySelectorAll(`#${id} .dot`);
        if (n > slides.length) {
            indexes[index] = 1;
        }
        if (n < 1) {indexes[index] = 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[indexes[index]-1].style.display = "block";
        dots[indexes[index]-1].className += " active";
    }
</script>

当然,此代码可以重构或修改,但这是最简单的方法

您需要向每个滑块添加ID或唯一类,并将其传递给函数showSlides,在那里您需要找到属于ID的幻灯片。函数showSlidesid,n{var slides=document.querySelectorAll${ID}.mySlides;var dots=document.querySelectorAll${id}.dot;..}很抱歉,这对我来说没有多大意义。我如何给他们ID,我应该在代码中的什么地方添加ID
    * {box-sizing:border-box}

/* Slideshow container */
.slideshow-container {
  max-width: 100%;
  position: relative;
  margin: auto;
}

/* Hide the images by default */
.mySlides {
  display: none;
}

/* Next & previous buttons */
.prev, .next {
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  margin-top: -22px;
  padding: 16px;
  color: white;
  font-weight: bold;
  font-size: 18px;
  transition: 0.6s ease;
  border-radius: 0 3px 3px 0;
  user-select: none;
}

/* Position the "next button" to the right */
.next {
  right: 0;
  border-radius: 3px 0 0 3px;
}

.prev {
  left: 0;
  border-radius: 3px 0 0 3px;
}

/* 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);
}


/* The dots/bullets/indicators */
.dot {
  cursor: pointer;
  height: 15px;
  width: 15px;
  margin: 0 2px;
  background-color: #bbb;
  border-radius: 50%;
  display: inline-block;
  transition: background-color 0.6s ease;
}

.active, .dot:hover {
  background-color: #717171;
}

.fade {
  -webkit-animation-name: fade;
  -webkit-animation-duration: 1.5s;
  -webkit-animation-fill-mode: forwards; 
  animation-name: fade;
  animation-duration: 1.5s;
  animation-fill-mode: forwards; 
}

@-webkit-keyframes fade {
  from {
    opacity: .4
  }
  to {
    opacity: 1
  }
}
@keyframes fade {
  from {
    opacity: .4
  }
  to {
    opacity: 1
  }
}

</style>```

<div class="slideshow-container" id="first">

 <!-- Full-width images with number and caption text -->
     <div class="mySlides fade">
         <img src="/images/1.jpg" style="width:100%">
     </div>

     <div class="mySlides fade">
         <img src="/images/2.jpg" style="width:100%">
     </div>

     <div class="mySlides fade">
        <img src="/images/3.jpg">
     </div>

 <!-- Next and previous buttons -->
 <a class="prev" onclick="plusSlides(-1, 'first', 'firstSliderIndex')">&#10094;</a>
  <a class="next" onclick="plusSlides(1, 'first', 'firstSliderIndex')">&#10095;</a>
  <br />
  <!-- The dots/circles -->
<div style="text-align:center">
  <span class="dot" onclick="currentSlide(1, 'first', 'firstSliderIndex')"></span>
  <span class="dot" onclick="currentSlide(2, 'first', 'firstSliderIndex')"></span>
  <span class="dot" onclick="currentSlide(3, 'first', 'firstSliderIndex')"></span>
</div>
</div>
<script>
    var indexes = {
        firstSliderIndex: 1,
        secondSliderIndex: 2,
    };
    showSlides(indexes.firstSliderIndex, 'first', 'firstSliderIndex');
    // Next/previous controls
    function plusSlides(n, id, index) { // n - number of slide, id - container id, index - current slide number in slider
        showSlides(indexes[index] += n, id, index);
    }
    // Thumbnail image controls
    function currentSlide(n, id, index) {
        showSlides(indexes[index] = n, id, index);
    }

    function showSlides(n, id, index) {
        var i;
        var slides = document.querySelectorAll(`#${id} .mySlides`);
        var dots = document.querySelectorAll(`#${id} .dot`);
        if (n > slides.length) {
            indexes[index] = 1;
        }
        if (n < 1) {indexes[index] = 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[indexes[index]-1].style.display = "block";
        dots[indexes[index]-1].className += " active";
    }
</script>