如何将滑块项目符号链接到JavaScript中的相关幻灯片

时间:2018-08-07 13:21:25

标签: javascript html css

我正在尝试将滑块的项目符号链接到滑块。项目符号1 =幻灯片1,项目符号2 =幻灯片2等等。我只是不确定如何匹配它,我知道它应该很简单,但是我的大脑无法正常工作!我已经设置了基本的重置,以便每次单击项目符号时,它都会删除项目符号上的活动类和滑块幻灯片,但是现在我需要它,然后将相同的2类添加到单击的项目符号及其关联的幻灯片中。

对此表示赞赏。

var slideBullet = document.querySelectorAll('.slider__bullet');
for (var i = 0; i < slideBullet.length; i++) {
  slideBullet[i].addEventListener('click', function() {

    var sliderSlide = document.querySelectorAll('.slider__slide');

    function resetSlides() {
      for (var i = 0; i < sliderSlide.length; i++) {
        sliderSlide[i].classList.remove('active');
      }
      for (var i = 0; i < slideBullet.length; i++) {
        slideBullet[i].classList.remove('active');
      }
    }
  });
}
  .slider {
  width: 90%;
  height: 100vh;
  position: relative;
  border: 1px solid black;
  margin: 0 auto;
}

.slider__slides {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  display: none;
  text-align: center;
  margin: 0 auto;
}

.slider__slides.active {
  display: block;
}

.slider__bullets {
  position: absolute;
  bottom: 5%;
  left: 0;
  right: 0;
  margin-left: auto;
  margin-right: auto;
  display: flex;
  justify-content: center;
}

.slider__bullet {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background-color: #a3a3a3;
  cursor: pointer;
  margin-left: 10px;
}

.slider__bullet.active {
  background-color: grey;
  transform: scale(1.8);
<div class="slider">
  <div class="slider__slides active">1</div>
  <div class="slider__slides">2</div>
  <div class="slider__slides">3</div>
  <div class="slider__slides">4</div>
  <div class="slider__slides">5</div>
  <div class="slider__bullets">
    <div class="slider__bullet active"></div>
    <div class="slider__bullet"></div>
    <div class="slider__bullet"></div>
    <div class="slider__bullet"></div>
    <div class="slider__bullet"></div>
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

我更改了您的jquery代码, 尝试下面的代码,希望它对您有用

var slideIndex = 1;
showSlides(slideIndex);

function plusSlides(n) {
  showSlides(slideIndex += n);
}

function currentSlide(n) {
  showSlides(slideIndex = n);
}

function showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("slider__slides");
  var dots = document.getElementsByClassName("slider__bullet");
  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";
}
 .slider {
  width: 90%;
  height: 100vh;
  position: relative;
  border: 1px solid black;
  margin: 0 auto;
}

.slider__slides {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  display: none;
  text-align: center;
  margin: 0 auto;
}

.slider__slides.active {
  display: block;
}

.slider__bullets {
  position: absolute;
  bottom: 5%;
  left: 0;
  right: 0;
  margin-left: auto;
  margin-right: auto;
  display: flex;
  justify-content: center;
}

.slider__bullet {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background-color: #a3a3a3;
  cursor: pointer;
  margin-left: 10px;
}

.slider__bullet.active {
  background-color: grey;
  transform: scale(1.8);
  }
<div class="slider">
  <div class="slider__slides active">1</div>
  <div class="slider__slides">2</div>
  <div class="slider__slides">3</div>
  <div class="slider__slides">4</div>
  <div class="slider__slides">5</div>
  
  
  <div class="slider__bullets">
    <div class="slider__bullet active" onclick="currentSlide(1)"></div>
    <div class="slider__bullet" onclick="currentSlide(2)"></div>
    <div class="slider__bullet" onclick="currentSlide(3)"></div>
    <div class="slider__bullet" onclick="currentSlide(4)"></div>
    <div class="slider__bullet" onclick="currentSlide(5)"></div>
  </div>
</div>