繁体   English   中英

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

[英]JavaScript: How to add multiple image slider on one page with same script?

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

但是,据我所知,每个滑块 div 都应该有不同的 scripID,但我无法解决。

这是我的代码:

HTML:

<div class="card" class="col-md-3">
 <!-- Slideshow container -->
    <div class="slideshow-container">
     <!-- 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" style="width:100%">
         </div>

     <!-- Next and previous buttons -->
     <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
      <a class="next" onclick="plusSlides(1)">&#10095;</a>
    </div>
    <br>

    <!-- The dots/circles -->
    <div style="text-align:center">
      <span class="dot" onclick="currentSlide(1)"></span>
      <span class="dot" onclick="currentSlide(2)"></span>
      <span class="dot" onclick="currentSlide(3)"></span>
    </div>
 <h2>Header </h2>
 <hr>
 <p class="hinfo">headerinfo</p>
 <p>Text info on the card</p>
 <p>Grey & Walnut colors<br>W:120 • D:120 • H:77</p>
</div>

脚本:

var slideIndex = 1;
showSlides(slideIndex);

// Next/previous controls
function plusSlides(n) {
  showSlides(slideIndex += n);
}

// Thumbnail image controls
function currentSlide(n) {
  showSlides(slideIndex = n);
}

function showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("dot");
  if (n > slides.length) {slideIndex = 1}
  if (n < 1) {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[slideIndex-1].style.display = "block";
  dots[slideIndex-1].className += " active";
}
</script>

CSS:

    * {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>```

首先,您需要将 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>

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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