js轮播图

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>轮播</title>
    <style>
      .slideshow-container {
        max-width: 800px;
        position: relative;
        margin: auto;
      }

      .mySlides {
        display: none;
        width: 100%;
        height: 300px;
        background-color: #717171;
      }

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

      .next {
        right: 0;
        border-radius: 3px 0 0 3px;
      }

      .prev:hover,
      .next:hover {
        background-color: rgba(0, 0, 0, 0.8);
      }

      .dot-container {
        text-align: center;
        padding: 20px;
        background: #ddd;
      }

      .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 {
        animation-name: fade;
        animation-duration: 1.5s;
      }

      @keyframes fade {
        from {
          opacity: 0.4;
        }
        to {
          opacity: 1;
        }
      }
    </style>
  </head>
  <body>
    <div class="slideshow-container">
      <div class="mySlides fade">
        <p>aaaaaa</p>
      </div>

      <div class="mySlides fade">
        <p>bbbbbbbbb</p>
      </div>

      <div class="mySlides fade">
        <p>ccccccccc</p>
      </div>

      <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
      <a class="next" onclick="plusSlides(1)">&#10095;</a>
    </div>

    <div class="dot-container">
      <div class="dot" onclick="currentSlide(1)"></div>
      <div class="dot" onclick="currentSlide(2)"></div>
      <div class="dot" onclick="currentSlide(3)"></div>
    </div>
  </body>
  <script>
    let slideIndex = 1
    showSlides(slideIndex)

    // 箭头切换
    function plusSlides(n) {
      showSlides((slideIndex += n))
    }

    // 圆点切换
    function currentSlide(n) {
      showSlides((slideIndex = n))
    }

    function showSlides(n) {
      let i
      const slides = document.getElementsByClassName('mySlides')
      const 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'
    }

    // 自动播放
    let autoIndex = 0
    function autoSlides() {
      let i
      const slides = document.getElementsByClassName('mySlides')
      const dots = document.getElementsByClassName('dot')
      for (i = 0; i < slides.length; i++) {
        slides[i].style.display = 'none'
      }
      autoIndex++
      if (autoIndex > slides.length) {
        autoIndex = 1
      }
      slides[autoIndex - 1].style.display = 'block'
      for (i = 0; i < dots.length; i++) {
        dots[i].className = dots[i].className.replace(' active', '')
      }
      dots[autoIndex - 1].className += ' active'
      setTimeout(autoSlides, 2000)
    }

    autoSlides()
  </script>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值