js原生写个简单的轮播图

效果图
在这里插入图片描述
css

	 * {
        margin: 0;
        padding: 0;
      }
      .container {
        position: relative;
        width: 640px;
        height: 480px;
        margin: 50px auto;
        overflow: hidden;
      }
      ul {
        list-style: none;
      }
      .main {
        width: 500%;
      }
      .main li {
        float: left;
      }
      .main li img {
        width: 640px;
        height: 480px;
      }

      aside {
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        width: 50px;
        height: 80px;
        color: #ccc;
        background-color: #fff;
        font-size: 30px;
        line-height: 80px;
        text-align: center;
        display: none;
      }
      .container:hover aside {
        display: block;
      }
      aside:hover {
        color: blue;
      }
      .right {
        right: 0;
      }
      .dot {
        position: absolute;
        bottom: 50px;
        right: 100px;
      }
      .dot li {
        float: left;
        width: 20px;
        height: 20px;
        background-color: #fff;
        border-radius: 50%;
      }
      .dot li + li {
        margin-left: 20px;
      }
      .active {
        background-color: red !important;
      }

html

 <div class="container">
      <ul class="main">
        <li><img src="./img/1.jpg" alt="" /></li>
        <li><img src="./img/2.jpg" alt="" /></li>
        <li><img src="./img/3.jpg" alt="" /></li>
        <li><img src="./img/4.jpg" alt="" /></li>
        <li><img src="./img/5.jpg" alt="" /></li>
      </ul>
      <aside class="left"><</aside>
      <aside class="right">></aside>
      <ul class="dot">
        <li class="active"></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
      </ul>
    </div>

js

	   //获取父级容器
        var oCon = document.getElementsByClassName("container")[0];
        // 获取所有ul
        var oUl = document.getElementsByTagName("ul");
        // 获取第二个ul指示点
        var oDot = oUl[1].getElementsByTagName("li");
        // 两侧按钮
        var aside = document.getElementsByTagName("aside");
        var index = 0,  // 轮播索引
        timer = setInterval(function() {  // 开启计时器
          animate();
        }, 1000);
        // 左右侧动画
        function animate(direction = "R") {
          if (direction === "R") {
            ++index;
          } else {
            --index;
          }
          if (index == -1) {
            index = 4;
          } else if (index == 5) {
            index = 0;
          }
          changeDot(); // 更改点的位置
          oUl[0].style.transform = "translateX(-" + 20 * index + "%)"; //幻灯片位置改变
        }
        // 鼠标移入事件
        oCon.onmouseover = function() {
          clearInterval(timer);
        };
        // 鼠标移出事件
        oCon.onmouseout = function() {
          timer = setInterval(function() {
            animate("R");
          }, 1000);
        };
        // 点击左侧按钮
        aside[0].onclick = function() {
          animate("L");
        };
        // 点击右侧按钮
        aside[1].onclick = function() {
          animate("R");
        };
        // 改变点的位置
        function changeDot() {
          for (var i = 0; i < oDot.length; i++) {
            if (i == index) {
              oDot[index].classList.add("active");
            } else {
              oDot[i].classList.remove("active");
            }
          }
        }
        // 为所有点加点击事件
        for (let i = 0; i < oDot.length; i++) {
              oDot[i].onclick = function () {
                  index = i - 1; // animate索引默认加1,所以这里减去
                  animate("R");
              }
          }
好的,以下是一个简单的用原生js实现的轮播图: HTML代码: ```html <div class="slider"> <ul class="slides"> <li><img src="https://picsum.photos/id/1/800/400" alt="Slide 1"></li> <li><img src="https://picsum.photos/id/2/800/400" alt="Slide 2"></li> <li><img src="https://picsum.photos/id/3/800/400" alt="Slide 3"></li> </ul> </div> <button class="prev">Previous slide</button> <button class="next">Next slide</button> ``` CSS代码: ```css .slider { position: relative; } .slides { list-style: none; margin: 0; padding: 0; position: relative; } .slides li { position: absolute; left: 0; top: 0; opacity: 0; transition: opacity 1s ease-out; } .slides li:first-child { opacity: 1; } .prev, .next { position: absolute; top: 50%; transform: translateY(-50%); border: none; background: transparent; outline: none; cursor: pointer; } .prev { left: 20px; } .next { right: 20px; } ``` JavaScript代码: ```javascript const slides = document.querySelectorAll('.slides li'); const prevBtn = document.querySelector('.prev'); const nextBtn = document.querySelector('.next'); let currentIndex = 0; // 显示当前索引的幻灯片 function showSlide(index) { // 将所有幻灯片的不透明度设置为0 slides.forEach(slide => { slide.style.opacity = 0; }); // 将当前幻灯片的不透明度设置为1 slides[index].style.opacity = 1; } // 显示下一张幻灯片 function nextSlide() { currentIndex++; if (currentIndex >= slides.length) { currentIndex = 0; } showSlide(currentIndex); } // 显示上一张幻灯片 function prevSlide() { currentIndex--; if (currentIndex < 0) { currentIndex = slides.length - 1; } showSlide(currentIndex); } // 添加按钮点击事件处理程序 prevBtn.addEventListener('click', prevSlide); nextBtn.addEventListener('click', nextSlide); ``` 以上代码将创建一个具有前进和后退按钮的简单幻灯片,导航按钮和计时器等其他功能可根据需要添加。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值