原生JS利用transform实现banner的无限滚动

  发布时间:2025-11-05 06:30:38   作者:玩站小弟   我要评论
功能默认情况无限循环向右移动 点击数字切换到对应图片 点击左右切换可切换图片 原理首先说下原理。在布局上所有的图片都是重叠的,即只要保证Y方向对齐即可,当前可 。

 功能

 默认情况无限循环向右移动  点击数字切换到对应图片  点击左右切换可切换图片

原理

首先说下原理。原生

 在布局上所有的利用图片都是重叠的,即只要保证Y方向对齐即可,无限当前可见的滚动图z-index层级最高。  每隔3s中更换一张图片,原生使用setTimeout定时。利用  使用gIndex记录当前可视区域的无限展示的是哪张图片下标,每次更换,滚动计算下一张图片的原生下标。  通过requestAnimationFrame实现一次图片切换的利用动画。免费信息发布网

这种方法也可以做到整个页面始终只有2个img标签,无限而不必把所有的滚动img节点全部创建出来,要点是原生每次更换不可见img的src。

动画的利用实现

 首先定义一个timestap,这个值记录每个帧移动多少距离。无限定义初始step=0,记录移动的步数。  每次移动的距离moveWidth是timestamp*step,图片1向右移动增加moveWidth,图片2从左侧进入moveWidth。因此,云服务器图片1的transform是translate(moveWidth), 而图片2的transform则是translate(moveWidth-图片宽度)。

      3.  step+1

      4.  如果moveWidth>图片宽度,步骤5,否则requestAnimationFrame请求下一次执行,继续2-4.

      5.  图片1和2都将位置放置在起始位置,图片2的z-index设置为最高。

这样就完成了一次移动的动画。

html代码 

<header>      <div class="box">          <img src="imgs/banner1.jpg">          <img src="imgs/banner2.jpg">          <img src="imgs/banner3.jpg">          <img src="imgs/banner4.jpg">      </div>      <div class="buttons">          <div class="active">1</div>          <div>2</div>          <div>3</div>          <div>4</div>      </div>      <div class="left">          <div class="arrow"></div>      </div>      <div class="right">          <div class="arrow"></div>      </div>  </header> 

JS代码 

var timeout = null;  window.onload = function () {      var oLeft = document.querySelector(.left);      var oRight = document.querySelector(.right);      var oButton = document.querySelector(.buttons);      var oButtons = document.querySelectorAll(.buttons div);      var oImgs = document.querySelectorAll(.box img);      var imgWidth = oImgs[0].width;      var gIndex = 0;      begainAnimate();      // 绑定左右点击事件      oLeft.onclick = function () {          clearTimeout(timeout);          leftMove();          begainAnimate();      };      oRight.onclick = function () {          clearTimeout(timeout);          rightMove();          begainAnimate();      };      // 绑定数字序号事件      oButton.onclick = function (event) {          clearTimeout(timeout);          var targetEl = event.target;          var nextIndex = (+targetEl.innerText) - 1;          console.log(nextIndex);          rightMove(nextIndex);          begainAnimate();      }      // 默认初始动画朝右边      function begainAnimate() {          clearTimeout(timeout);          timeout = setTimeout(function () {              rightMove();              begainAnimate();          }, 3000);      }      // 向左移动动画      function leftMove() {          var nextIndex = (gIndex - 1 < 0) ? oImgs.length - 1 : gIndex - 1;          animateSteps(nextIndex, -50);      }      // 向右移动动画      function rightMove(nextIndex) {          if (nextIndex == undefined) {              nextIndex = (gIndex + 1 >= oImgs.length) ? 0 : gIndex + 1;          }          animateSteps(nextIndex, 50);      }      // 一次动画      function animateSteps(nextIndex, timestamp) {          var currentImg = oImgs[gIndex];          var nextImg = oImgs[nextIndex];          nextImg.style.zIndex = 10;          var step = 0;          requestAnimationFrame(goStep);          // 走一帧的动画,移动timestamp          function goStep() {              var moveWidth = timestamp * step++;              if (Math.abs(moveWidth) < imgWidth) {                  currentImg.style.transform = `translate(${moveWidth}px)`;                  nextImg.style.transform = `translate(${moveWidth > 0 ? (moveWidth - imgWidth) : (imgWidth + moveWidth)}px)`;                  requestAnimationFrame(goStep);              } else {                  currentImg.style.zIndex = 1;                  currentImg.style.transform = `translate(0px)`;                  nextImg.style.transform = `translate(0px)`;                  oButtons[gIndex].setAttribute(class, );                  oButtons[nextIndex].setAttribute(class, active);                  gIndex = nextIndex;              }          }      }  }  window.onclose = function () {      clearTimeout(timeout);  } 

css布局样式 

<style>      /* 首先设置图片box的区域,将图片重叠在一起  */      header {          width: 100%;          position: relative;          overflow: hidden;      }      .box {          width: 100%;          height: 300px;      }      .box img {          width: 100%;          height: 100%;          position: absolute;          transform: translateX(0);          z-index: 1;      }      .box img:first-child {          z-index: 10;      }        /* 数字序列按钮 */      .buttons {          position: absolute;          right: 10%;          bottom: 5%;          display: flex;          z-index: 100;      }      .buttons div {          width: 30px;          height: 30px;          background-color: #aaa;          border: 1px solid #aaa;          text-align: center;          margin: 10px;          cursor: pointer;          opacity: .7;          border-radius: 15px;          line-height: 30px;      }      .buttons div.active {          background-color: white;      }      /* 左右切换按钮 */      .left,      .right {          position: absolute;          width: 80px;          height: 80px;          background-color: #ccc;          z-index: 100;          top: 110px;          border-radius: 40px;          opacity: .5;          cursor: pointer;      }      .left {          left: 2%;      }     .right {          right: 2%;      }      .left .arrow {          width: 30px;          height: 30px;          border-left: solid 5px #666;          border-top: solid 5px #666;          transform: translate(-5px, 25px) rotate(-45deg) translate(25px, 25px);      }      .right .arrow {          width: 30px;          height: 30px;          border-left: solid 5px #666;          border-top: solid 5px #666;          transform: translate(50px, 25px) rotate(135deg) translate(25px, 25px);      }  </style>  香港云服务器
  • Tag:

相关文章

  • Pixelbook教程(探索Pixelbook的功能与技巧,体验极致的电脑操作体验)

    摘要:随着科技的不断发展,电脑已经成为了人们工作和娱乐生活中必不可少的一部分。而谷歌的Pixelbook作为一款强大而多功能的笔记本电脑,以其独特的设计和出色的性能受到了众多用户的喜爱。...
    2025-11-05
  • 注册域名最重要的因素是什么?

    正所谓“工欲善其事,必先利其器”,对于互联网企业来说域名就是一个企业的品牌,也是一个企业发展壮大的根基。因此,在注册域名的时候一定要给企业选择最合适的域名,那么什么是最合适的域名呢?注册域名时我们最关
    2025-11-05
  • Kitty-Cloud中的编码规范

    项目地址https://github.com/yinjihuan/kitty-cloud[1]规范定义每个公司,每个团队都有不同的规范,有的人喜欢这样,有的人喜欢那样。所以一个团队中最好有明确的一份规
    2025-11-05
  • 被吐槽 GitHub仓 库太大,直接 600M 瘦身到 6M,这下舒服了

    大家好,我是小富~前言忙里偷闲学习了点技术写了点demo代码,打算提交到我那 2000Star的Github仓库上,居然发现有5个Issues,最近的一条日期已经是2022/8/1了,以前我还真没留意
    2025-11-05
  • 探索oppocoloros2.0(从界面设计到个性化定制,发现coloros2.0的无限魅力)

    摘要:作为oppo手机的操作系统,coloros2.0带来了全新的视觉体验和个性化定制功能。这篇文章将深入探索coloros2.0的各项特色和功能,为读者展示其无限魅力。标题和...
    2025-11-05
  • 双拼域名为何能在域名界崭露头角?

    在互联网时代,域名代表企业在网络上的形象。而纵观国内各大企业,官网启用的大多是双拼域名,比如聚名网juming.com、新浪微博weibo.com以及知乎zhihu.com等等。这主要得益于中国网民受
    2025-11-05

最新评论