用原生js和transform动画手撸一个轮播图

本文介绍了一个使用纯HTML、CSS和JavaScript实现的轮播图,带有时间轴功能,支持自动和手动切换,且易于组件化。代码展示了如何控制背景颜色和处理左右切换按钮,但图片需要用户自定义替换。
摘要由CSDN通过智能技术生成

本代码意在实现一个带有时间轴的轮播图,时间轴可以跟随轮播图一起滚动。整体轮播图可以自动轮播亦可点击左右按钮进行手动切换,亦可用户误无操作5s后自动轮播。

代码如下

HTML代码

<div class="outside">
      <div class="box">
        <div class="box_top">
          <div class="box_top_inner" id="big">
            <div class="box_top_inner_item">
              <div class="time">2023</div>
              <div class="dotted"></div>
            </div>
            <div class="box_top_inner_item">
              <div class="time">2022</div>
              <div class="dotted"></div>
            </div>
            <div class="box_top_inner_item">
              <div class="time">2021</div>
              <div class="dotted"></div>
            </div>
            <div class="box_top_inner_item">
              <div class="time">2020</div>
              <div class="dotted"></div>
            </div>
            <div class="box_top_inner_item">
              <div class="time">2019</div>
              <div class="dotted"></div>
            </div>
            <div class="box_top_inner_item">
              <div class="time">2018</div>
              <div class="dotted"></div>
            </div>
          </div>
          <div class="box_top_dotted"></div>
        </div>
        <div class="box_bottom">
          <div class="box_bottom_inner" id="boxAll">
            <div class="box_bottom_item">0</div>
            <div class="box_bottom_item">1</div>
            <div class="box_bottom_item">2</div>
            <div class="box_bottom_item">3</div>
            <div class="box_bottom_item">4</div>
            <div class="box_bottom_item">5</div>
          </div>
          <div class="box_bottom_left" id="goLeft">
            <img src="./images/pause.png" alt="" />
          </div>
          <div class="box_bottom_right" id="goRight">
            <img src="./images/pause.png" alt="" />
          </div>
        </div>
      </div>
    </div>

css代码

* {
        margin: 0;
        padding: 0;
      }

      .outside {
        width: 100%;
        height: 100vh;
        display: flex;
        justify-content: center;
        align-items: center;
      }

      .box {
        width: 800px;
        height: 500px;
        border: 1px solid #ccc;
      }

      .box_top {
        width: 100%;
        height: 80px;
        position: relative;
        overflow: hidden;
      }
      .box_top_dotted {
        width: 100%;
        height: 1px;
        border-bottom: 5px dotted #ffcc99;
        position: absolute;
        left: 0;
        bottom: 2px;
      }
      .box_top_inner {
        display: flex;
        position: absolute;
        top: 0;
        left: 338px;
        transition-duration: 1s;
        transition-timing-function: linear;
      }
      .box_top_inner_item {
        font-size: 30px;
        width: 125px;
        height: 80px;
        position: relative;
      }

      .time {
        width: 125px;
        height: 80px;
        text-align: center;
        line-height: 80px;
        font-size: 30px;
        color: #ffcc99;
      }

      .dotted {
        width: 10px;
        height: 10px;
        border-radius: 10px;
        background-color: #ffcc99;
        position: absolute;
        bottom: 0px;
        left: 57px;
      }
      .box_bottom {
        width: 100%;
        height: 420px;
        background-color: #ccc;
        position: relative;
      }
      .box_bottom {
        width: 100%;
        height: 420px;
        overflow: hidden;
        position: relative;
      }
      .box_bottom_inner {
        transition: all 1s;
        transition-timing-function: linear;
        display: flex;
      }
      .box_bottom_item {
        width: 800px !important;
        height: 420px;
      }
      .box_bottom_item:nth-child(1) {
        background-color: red;
      }
      .box_bottom_item:nth-child(2) {
        background-color: orange;
      }
      .box_bottom_item:nth-child(3) {
        background-color: yellow;
      }
      .box_bottom_item:nth-child(4) {
        background-color: green;
      }
      .box_bottom_item:nth-child(5) {
        background-color: blue;
      }
      .box_bottom_item:nth-child(6) {
        background-color: rgb(55, 161, 64);
      }
      .box_bottom_left {
        width: 60px;
        height: 60px;
        position: absolute;
        left: 0px;
        top: 190px;
      }
      .box_bottom_left img {
        width: 60px;
        height: 60px;
        transform: rotateY(180deg);
      }
      .box_bottom_right {
        width: 60px;
        height: 60px;
        position: absolute;
        right: 0px;
        top: 190px;
      }
      .box_bottom_right img {
        width: 60px;
        height: 60px;
      }

js代码

<script>
      var flag = 0;
      var time = 5;
      var bigInterval = null;
      var animationTime = null;
      function animation() {
        clearInterval(animationTime);
        animationTime = null;
        document.getElementById("boxAll").style.width = `${800 * 6}px`;
        bigInterval = setInterval(() => {
          if (flag < 5) {
            flag++;
            document.getElementById("big").style.transform = `translateX(-${
              125 * flag
            }px)`;
            document.getElementById("boxAll").style.transform = `translateX(-${
              800 * flag
            }px)`;
          } else {
            document.getElementById(
              "big"
            ).style.transform = `translateX(${0}px)`;
            flag = 0;
            document.getElementById(
              "boxAll"
            ).style.transform = `translateX(-${0}px)`;
          }
          var arrLength = document.getElementsByClassName("time").length;
          for (var i = 0; i < arrLength; i++) {
            document.getElementsByClassName("time")[i].style.fontSize = "25px";
          }
          setTimeout(() => {
            document.getElementsByClassName("time")[flag].style.fontSize =
              "40px";
          }, 1000);
        }, 2000);
      }
      animation();
      document.getElementById("goRight").onclick = function () {
        time = 5;
        clearInterval(animationTime);
        animationTime = null;
        openAnimation();
        clearInterval(bigInterval);
        bigInterval = null;
        flag++;
        if (flag < 5) {
          document.getElementById("big").style.transform = `translateX(-${
            125 * flag
          }px)`;
          document.getElementById("boxAll").style.transform = `translateX(-${
            800 * flag
          }px)`;
        } else {
          document.getElementById("big").style.transform = `translateX(${0}px)`;
          flag = 0;
          document.getElementById(
            "boxAll"
          ).style.transform = `translateX(-${0}px)`;
        }
        var arrLength = document.getElementsByClassName("time").length;
        for (var i = 0; i < arrLength; i++) {
          document.getElementsByClassName("time")[i].style.fontSize = "25px";
        }
        setTimeout(() => {
          document.getElementsByClassName("time")[flag].style.fontSize = "40px";
        }, 1000);
      };
      document.getElementById("goLeft").onclick = function () {
        clearInterval(animationTime);
        animationTime = null;
        time = 5;
        openAnimation();
        clearInterval(bigInterval);
        bigInterval = null;
        var arrLength =
          Number(document.getElementsByClassName("time").length) - 1;
        flag--;
        if (flag > -1) {
          document.getElementById("big").style.transform = `translateX(-${
            125 * flag
          }px)`;
          document.getElementById("boxAll").style.transform = `translateX(-${
            800 * flag
          }px)`;
        } else {
          document.getElementById("big").style.transform = `translateX(-${
            arrLength * 125
          }px)`;
          flag = 5;
          document.getElementById("boxAll").style.transform = `translateX(-${
            arrLength * 800
          }px)`;
        }
        for (var i = 0; i < arrLength; i++) {
          document.getElementsByClassName("time")[i].style.fontSize = "25px";
        }
        setTimeout(() => {
          document.getElementsByClassName("time")[flag].style.fontSize = "40px";
        }, 1000);
      };
      //计时开启动画
      function openAnimation() {
        animationTime = setInterval(() => {
          if (time > 0) {
            time--;
          } else {
            animation();
          }
        }, 1000);
      }
    </script>

本代码由于使用纯原生写的,没有引入任何插件,所以本代码完全可以改动为公共组件进行使用,难度并不大。由于轮播图我使用的是背景颜色,以后需要更改为文字内容或者图片都可以的。需要注意的是左右按钮是个图片,需要读者自行寻找图片更换。

  • 9
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个用HTML,CSS和JavaScript编写的简单轮播图,其中图片切换时包含淡入淡出效果的实现: HTML: ```html <div class="slideshow-container"> <div class="slide fade"> <img src="img1.jpg"> </div> <div class="slide fade"> <img src="img2.jpg"> </div> <div class="slide fade"> <img src="img3.jpg"> </div> <a class="prev" onclick="plusSlides(-1)">❮</a> <a class="next" onclick="plusSlides(1)">❯</a> </div> ``` CSS: ```css .slideshow-container { position: relative; margin: auto; } .slide { display: none; position: absolute; top: 0; left: 0; width: 100%; height: 100%; } .fade { animation-name: fade; animation-duration: 1.5s; } @keyframes fade { from { opacity: 0; } to { opacity: 1; } } .prev, .next { position: absolute; top: 50%; width: auto; margin-top: -22px; padding: 16px; 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); } ``` JavaScript: ```javascript 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("slide"); 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].classList.remove("fade"); slides[i].style.display = "none"; } slides[slideIndex - 1].style.display = "block"; slides[slideIndex - 1].classList.add("fade"); } ``` 这个轮播图一个包含淡入淡出效果的动画,通过CSS中的“fade”类实现。在JavaScript中,“showSlides”函数用于显示当前幻灯片,它将所有幻灯片的显示方式设置为“none”,然后将要显示的幻灯片的显示方式设置为“block”,并将其“fade”类添加到其类列表中,以触发淡入淡出效果。同时,“plusSlides”函数和“currentSlide”函数用于向前或向后移动幻灯片,或直接转到指定幻灯片。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值