如何以编程方式向滑块动态添加点?

时间:2018-07-20 14:19:38

标签: javascript javascript-events dom-events

我正在使用普通JavaScript ES5构建滑块。

我稍后将滑块上传到WordPress,因此用户需要能够上传图像,如果将有10张图像,则应显示10点。

因此,每张图片都要创建一个新的li项目,其类别为“点”。

我如何使其工作?

这是我的代码笔:Local Resolver

HTML:

<span class="slider-btn-prev">PREV</span>
<span class="slider-btn-next">NEXT</span>
<div class="controls">
</div>
<section class="slider ">
  <div class="slider--inner">
    <!-- <img src="http://via.placeholder.com/1750x450"> -->
    <ul class="slider-container">
      <li class="slider-item">
        <div class="slider-caption">
          <h2 class="slider-item__title">Mexican Grill</h2>
          <div>
            <ul class="slider-details">
              <li class="slider-caption__info">
                <i class="fa fa-cutlery"></i>
                <a href="" rel="tag">For Family</a>
              </li>
              <li class="slider-caption__info">
                <i class="fa fa-clock-o"></i>
                <a href="" rel="tag">30 min</a>
              </li>
              <li class="slider-caption__info">
                <i class="fa fa-user"></i> by Dorota
              </li>
            </ul>
          </div>
          <a class="btn btn--carousel" href="single-recipte.html">View Recipt</a>
        </div>
      </li>
      <!-- /item -->
      <li class="slider-item showing">
        <div class="slider-caption">
          <h2 class="slider-item__title">Olive Meals</h2>
          <div>
            <ul class="slider-details">
              <li class="slider-caption__info">
                <i class="fa fa-cutlery"></i>
                <a href="" rel="tag">For Family</a>
              </li>
              <li class="slider-caption__info">
                <i class="fa fa-clock-o"></i>
                <a href="" rel="tag">15 min</a>
              </li>
              <li class="slider-caption__info">
                <i class="fa fa-user"></i> by Dorota
              </li>
            </ul>
          </div>
          <a class="btn btn--carousel" href="single-recipte.html">View Recipt</a>
        </div>
      </li>
      <li class="slider-item">
        <div class="slider-caption">
          <h2 class="slider-item__title">Rosted Chicken</h2>
          <div>
            <ul class="slider-details">
              <li class="slider-caption__info">
                <i class="fa fa-cutlery"></i>
                <a href="" rel="tag">For Family</a>
              </li>
              <li class="slider-caption__info">
                <i class="fa fa-clock-o"></i>
                <a href="" rel="tag">75 min</a>
              </li>
              <li class="slider-caption__info">
                <i class="fa fa-user"></i> by Dorota
              </li>
            </ul>
          </div>
          <a class="btn btn--carousel" href="single-recipte.html">View Recipt</a>
        </div>
      </li>
    </ul>
  </div>
</section>

JS:

document.addEventListener('DOMContentLoaded', function() {
  var slides = document.querySelectorAll('.slider-item'),
    currentSlide = 0,
    slideInterval = setInterval(nextSlide, 6500);

  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";
    }

    slides[slideIndex - 1].style.display = "block";
    dots[slideIndex - 1].className += " active";
  }

  // slides.forEach(function(dot) {
  //      var controls = document.querySelector('.controls');
  //      dot.document.createElement("li");
  //      controls.appendChild(dots);
  //  });

  //   for (var i = 0; i < slides.length; i++) {
  //     var controls = document.querySelector('.controls');
  //     var dots = slides[i].document.createElement("li");
  //     controls.appendChild(dots);
  // };

  function nextSlide() {
    goToSlide(currentSlide + 1);
  }

  function previousSlide() {
    goToSlide(currentSlide - 1);
  }

  function goToSlide(n) {
    slides[currentSlide].className = 'slider-item';
    currentSlide = (n + slides.length) % slides.length;
    slides[currentSlide].className = 'slider-item showing';
  }

  var slideBtnNext = document.querySelector(".slider-btn-next"),
    slideBtnPrev = document.querySelector(".slider-btn-prev");


  slideBtnNext.addEventListener('click', function() {
    nextSlide();
  });

  slideBtnPrev.addEventListener('click', function() {
    previousSlide();
  });
});

0 个答案:

没有答案