显示相应的幻灯片时,在分页上添加活动类



我有一个简单的图像滑块,带有分页控件。我已经对其进行了编码以在分页上添加和删除"活动"类点击按钮。我也希望他们在显示相应的幻灯片时拥有活动类。如何修改代码以实现此目标?

<div id="slideshow">
<ul id="slides">
  <li class="slide showing">
    <div class="slide-description">
      <h1 class="slide-title">All-in-one EV charging solution.</h1>
      <p> Easy to use. Connected with smart charging capabilities. Our charging stations can be used at home,
        work or in public.</p>
      </div>
    </li>
    <li class="slide">
      <div class="slide-description">
        <h1 class="slide-title">Charging at work- a case study.</h1>
        <p>In this new series of charging case studies, we dive into into the reasons why EV-Box partners are taking the green route.</p>
      </div>
    </li>
    <li class="slide">  <div class="slide-description">
      <h1 class="slide-title">Finding the best solution for your charging routine.</h1>
      <p>
        This whitepaper highlights the key answers that will guide you to acharging solution that best serves your needs.
      </p>
    </div>
  </li>
</ul>

    <button class="controls" id="previous"></button>
  <button class="controls" id="next"></button>
  <div id="pagination"></div>

var slides = document.querySelectorAll('#slides .slide'); // get all the slides
var currentSlide = 0;
var slideInterval = setInterval(nextSlide, 6000);
function nextSlide() {
  goToSlide(currentSlide + 1);
}
function previousSlide() {
  goToSlide(currentSlide - 1);
}
function goToSlide(n) {
  slides[currentSlide].className = 'slide';
  currentSlide = (n + slides.length) % slides.length;
  slides[currentSlide].className = 'slide showing';
}
//Previous and Next controls
var next = document.getElementById('next');
var previous = document.getElementById('previous');
next.onclick = function() {
  nextSlide();
};
previous.onclick = function() {
  previousSlide();
};
//Pagination controls
var p = document.getElementById('pagination');
var phtml = '';
for(var i = 0; i < slides.length; i++) {
  phtml += '<button></button>'; // create the pagination buttons for each slide
}
p.innerHTML = phtml; //insert the html for the buttons
var pbuttons = p.querySelectorAll('button'); // grab all the buttons
var activeButton = null; // reference to active button
for(var i = 0; i < pbuttons.length; i++) {
  pbuttons[i].onclick = (function(n) {
    return function() {
      if(activeButton)
      activeButton.classList.remove('active'); // delete class from old active button
      activeButton = this;// change ref, this is current button
      activeButton.classList.add('active');// add class for new
      goToSlide(n);
    };
  })(i);
}

#slider {
  min-height: 400px;
  position: relative;
  #slides {
    min-height: 400px;
    padding: 0;
    margin: 0;
    list-style-type: none;
    .slide {
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      opacity: 0;
      z-index: 1;
      min-height: 400px;
      box-sizing: border-box;
      background: $black;
      color: $white;
      display: flex;
      align-items: center;
      -webkit-justify-content: flex-end;
      justify-content: flex-end;
      -webkit-transition: opacity 1s;
      -moz-transition: opacity 1s;
      transition: opacity 1s;
      &.showing {
        opacity: 1;
        z-index: 2;
      }
      &:nth-of-type(1) {
        @include backImage('../images/evbox1.jpg');
      }
      &:nth-of-type(2){
        @include backImage('../images/evbox2.jpg');
      }
      &:nth-of-type(3) {
        @include backImage('../images/evbox3.jpg');
      }
    }
    .slide-description {
      width: 500px;
      .slide-title {
        width: 90%;
        font-family: 'Open Sans', sans-serif;
        font-size: 44px;
        text-shadow: 0 2px 3px rgba($black, .2);
        line-height: 1.1em;
        margin-bottom: 10px;
      }
      p {
        margin-bottom: 15px;
        width: 90%;
        font-size: 20px;
        font-family: 'Open Sans', sans-serif;
        font-weight: 300;
        text-shadow: 0 2px 3px rgba($black, .2);
      }
      .btn {
        @include button($blue, $font-color, $shadow-color);
      }
    }
    @include respond-to($mobile) {
      @media only screen and (max-width: $mobile) {
        .slide:nth-of-type(3) {
          background-position: left 0;
        }
        .slide-description {
          height: 100%;
          width: 100%;
          padding: 8em 5em;
          position: static;
          text-align: center;
          .slide-title {
            font-size: 30px;
            width: 100%;
          }
          .btn {
            padding: 8px 16px;
          }
        }
        p {
          display: none;
        }
      }
    }
  }
}

/*Previous and Next Controls*/
.controls {
  position: absolute;
  top: 42%;
  z-index: 10;
  background: url('http://www.ev-box.com/Evbox-EN/includes/themes/evbox/assets/images/sprites@2x.png') no-repeat;
  height: 50px;
  width: 30px;
  background-size: 369px 240px;
  outline: 0;
  border: 0;
  cursor: pointer;
}
#previous {
  right: 10px;
  background-position: -50px -121px;
}
#next {
  left: 10px;
  background-position: -16px -121px;
}
// Pagination
#pagination {
  position: absolute;
  bottom: 30px;
  right: 50%;
  z-index: 10;
  button {
    border-radius: 50%;
    border: 1px solid $white;
    background-color: $white;
    opacity: .8;
    width: 14px;
    height: 14px;
    min-height: 14px;
    border-width: 0;
    margin-right: 5px;
    &.active {
      width: 15px;
      height: 15px;
      min-height: 15px;
      border: 1px solid $white;
      background-color: $primary;
      opacity: 1;
      &:focus {
        outline: 0;
      }
    }
  }
}

我更新了JavaScript,如下所示。在您的代码笔中使用它。您可以看到差异。如果单击下一步和上一个按钮,则使用此JavaScript

可以突出显示相应的分页
document.addEventListener("DOMContentLoaded", function() {

var slides = document.querySelectorAll('#slides .slide'); // get all the slides
const totalSlides = 2 // Total number of slides count
var currentSlide = 0; // current slide 
var previousSlide = totalSlides;
 // var slideInterval = setInterval(nextSlide, 1000);
 function nextSlide() {
   previousSlide = currentSlide
   if(totalSlides > currentSlide){
   currentSlide += 1
 }
   else currentSlide = 0
   goToSlide();
 }
function PreviousSlide() {
      if(currentSlide == 0){
        previousSlide = currentSlide
        currentSlide = totalSlides
      }
      else{
          previousSlide = currentSlide
         currentSlide -=1
       }
      goToSlide();
}

function goToSlide() {
  slides[previousSlide].className = 'slide';
  slides[currentSlide].className = 'slide showing';
  var currentSlideButton = "button"+currentSlide
  var previousSlideButton = "button"+previousSlide
  document.getElementById(currentSlideButton).classList.add('active');
  document.getElementById(previousSlideButton).classList.remove('active');
}
//Previous and Next controls
var next = document.getElementById('next');
var previous = document.getElementById('previous');
next.onclick = function() {
  PreviousSlide();
};
previous.onclick = function() {
  nextSlide();
};
//Pagination controls
var p = document.getElementById('pagination');
var phtml = '';
for(var i = 0; i < slides.length; i++) {
  phtml += '<button id=button'+i+'></button>'; // create the pagination buttons for each slide
}
p.innerHTML = phtml; //insert the html for the buttons
var pbuttons = p.querySelectorAll('button'); // grab all the buttons
var activeButton = null; // reference to active button
for(var i = 0; i < pbuttons.length; i++) {
  pbuttons[i].onclick = (function(n) {
    return function() {
      if(activeButton)
      activeButton.classList.remove('active'); // delete class from old active button
      activeButton = this;// change ref, this is current button
      activeButton.classList.add('active');// add class for new
      goToSlide(n);
    };
  })(i);
}
goToSlide();// Initialising goToSlide method
});

最新更新