资讯专栏INFORMATION COLUMN

利用js实现轮播图自动切换

3403771864 / 547人阅读

  实现轮播图自动切换就用JS,先看效果图

  第一种

</>复制代码

  1.   //点击按钮,序号变化
  2.   showIdx++;
  3.   if (showIdx == imgArr.length) {
  4.   showIdx = 0;
  5.   }
  6.   //所有盒子左移动
  7.   for (let i = 0; i <items.length; i++) {
  8.   items[i].style.left = parseFloat(items[i].style.left) - loopbox.offsetWidth + "px";
  9.   }
  10.   //冗余容器从页面中删除
  11.   //当冗余容器从页面中移除后,为了保证结构想对应,所以呀item中数组也要把这个容器删除
  12.   let deleteBox = items.shift();
  13.   // console.log(items);
  14.   deleteBox.remove();
  15.   //在用户看不到的内存中,变更【被从这个页面删除的元素的位置
  16.   deleteBox.style.left = "900px";
  17.   wrapper.appendChild(deleteBox);
  18.   items.push(deleteBox);
  19.   //把容器从小添加至压面后,容器加载的图片在当前的下一站
  20.   // 第七步 把容器重新添加至页面后,容器加载的图片要是当前这张的下一张
  21.   if (showIdx == imgArr.length - 1) {
  22.   deleteBox.innerHTML = `<img src=${imgArr[0]}>`;
  23.   } else {
  24.   deleteBox.innerHTML = `<img src=${imgArr[showIdx + 1]}>`;
  25.   }

  第二种,图片切换,css代码

</>复制代码

  1.   html,
  2.   body,
  3.   ul,
  4.   li {
  5.   margin: 0;
  6.   padding: 0;
  7.   }
  8.   a {
  9.   text-decoration: none;
  10.   }
  11.   .loopbox {
  12.   width: 1226px;
  13.   height: 460px;
  14.   background: #030;
  15.   position: relative;
  16.   overflow: hidden;
  17.   }
  18.   .box {
  19.   width: 100%;
  20.   height: 100%;
  21.   float: left;
  22.   transitionall .3s;
  23.   position: absolute;
  24.   left: 0;
  25.   /* overflow: hidden; */
  26.   }
  27.   .box.notran{
  28.   transition: none;
  29.   }
  30.   .box-item {
  31.   /* width: 100%; */
  32.   width: 1226px;
  33.   height: 100%;
  34.   float: left;
  35.   background: #f1f1f1;
  36.   text-align: center;
  37.   font-size: 24px;
  38.   line-height: 100px;
  39.   /* display: none; */
  40.   /* transition: all .3s; */
  41.   }
  42.   .box-item img {
  43.   width: 100%;
  44.   height: 100%;
  45.   /* 图片适应 */
  46.   object-fit: cover;
  47.   }
  48.   .arrow {
  49.   width: 60px;
  50.   line-height: 30px;
  51.   background: #f00;
  52.   text-align: center;
  53.   color: #f1f1f1;
  54.   position: absolute;
  55.   top: 50%;
  56.   left: 10px;
  57.   margin-top: -15px;
  58.   border-radius: 15px;
  59.   }
  60.   .arrow:hover {
  61.   background: #f60;
  62.   }
  63.   .arrow.arrow-right {
  64.   left: auto;
  65.   right: 10px;
  66.   }
  67.   .page {
  68.   position: absolute;
  69.   width: 100%;
  70.   text-align: center;
  71.   bottom: 10px;
  72.   }
  73.   .page li {
  74.   display: inline-block;
  75.   width: 80px;
  76.   height: 8px;
  77.   border-radius: 4px;
  78.   background: #000;
  79.   }
  80.   /* .page li:first-child {
  81.   background: #f90;
  82.   } */
  83.   .page li:hover {
  84.   background: #f60;
  85.   }
  86.   .page li.current {
  87.   background: #f90;
  88.   }
  89.   .side-qq {
  90.   width: 100px;
  91.   height: 100px;
  92.   background: #f00;
  93.   /* position: fixed; */
  94.   position: absolute;
  95.   right: 10px;
  96.   top: 450px;
  97.   }
  98.   .navbar {
  99.   width: 100%;
  100.   background: #ccc;
  101.   text-align: center;
  102.   }
  103.   .navbar.fixed {
  104.   position: fixed;
  105.   left: 0;
  106.   top: 0;
  107.   }
  108.   .nav {
  109.   height: 21px;
  110.   }

  js

 

</>复制代码

  1.  <!DOCTYPE html>
  2.   <html>
  3.   <head>
  4.   <meta charset="UTF-8">
  5.   <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.   <title>Document</title>
  8.   <link rel="stylesheet" href="./css/index.css">
  9.   </head>
  10.   <body>
  11.   <!-- 1.分析页面结构 -->
  12.   <div>
  13.   <div id="box">
  14.   <div class="box-item curr"><img src="images/1.webp"></div>
  15.   <div><img src="images/2.webp"></div>
  16.   <div><img src="images/3.webp"></div>
  17.   <div><img src="images/4.webp"></div>
  18.   </div>
  19.   <a class="arrow arrow-left" href="javascript:;">左</a>
  20.   <a class="arrow arrow-right" href="javascript:;">右</a>
  21.   <ul id="page">
  22.   <li></li>
  23.   <li></li>
  24.   <li></li>
  25.   <li></li>
  26.   </ul>
  27.   </div>
  28.   <script>
  29.   // 1.5.初始化页面,保证所有的图片先拍成一排
  30.   let items = document.querySelectorAll(".box-item");
  31.   let lis = document.querySelectorAll(".page li");
  32.   let leftBtn=document.querySelector(".arrow-left")
  33.   let box = document.querySelector(".box");
  34.   let loopbox = document.querySelector(".loopbox");
  35.   box.style.width = items.length * loopbox.offsetWidth + "px";
  36.   box.style.left = 0+"px";
  37.   // 2.分析功能入口
  38.   let rightBtn = document.querySelector(".arrow-right");
  39.   let showIdx = 0;
  40.   rightBtn.onclick = function (){
  41.   items[showIdx].classList.remove("curr");
  42.   lis[showIdx].classList.remove("current");
  43.   showIdx ++;
  44.   if(showIdx == 4){
  45.   showIdx = 0;
  46.   }
  47.   items[showIdx].classList.add("curr");
  48.   lis[showIdx].classList.add("current");
  49.   box.style.left = (-1) * showIdx * loopbox.offsetWidth + "px";
  50.   for(let i=0;i<lis.length;i++){
  51.   lis[i].onclick =function(){
  52.   items[showIdx].classList.remove("curr");
  53.   lis[showIdx].classList.remove("current");
  54.   showIdx=i;
  55.   items[showIdx].classList.add("curr");
  56.   lis[showIdx].classList.add("current");
  57.   }
  58.   }
  59.   leftBtn.onclick = function(){
  60.   //第一张 消失(取消类名)
  61.   items[showIdx].classList.remove("curr");
  62.   lis[showIdx].classList.remove("current");
  63.   showIdx --;
  64.   //预知判断
  65.   if(showIdx == -1){
  66.   //点击之后,点击之前意味着已经在加,需要归零
  67.   showIdx = 3;
  68.   }
  69.   items[showIdx].classList.add("curr");
  70.   lis[showIdx].classList.add("current");
  71.   box.style.left = (-1) * showIdx * loopbox.offsetWidth + "px";
  72.   // 第二张 出现(添加类名)showIdx+1
  73.   };
  74.   for(let j=0;j<lis.length;j++){
  75.   lis[j].onclick = function(){
  76.   items[showIdx].classList.remove("curr");
  77.   lis[showIdx].classList.remove("current");
  78.   //选好变为点击序号对应结构
  79.   showIdx=j;
  80.   items[showIdx].classList.add("curr");
  81.   lis[showIdx].classList.add("current");
  82.   }
  83.   }
  84.   }
  85.   function time(){
  86.   items[showIdx].classList.remove("curr");
  87.   lis[showIdx].classList.remove("current");
  88.   showIdx ++;
  89.   if(showIdx == 4){
  90.   showIdx = 0;
  91.   }
  92.   items[showIdx].classList.add("curr");
  93.   lis[showIdx].classList.add("current");
  94.   box.style.left = (-1) * showIdx * loopbox.offsetWidth + "px";
  95.   }
  96.   for(let i=0;i<lis.length;i++){
  97.   lis[i].onclick =function(){
  98.   items[showIdx].classList.remove("curr");
  99.   lis[showIdx].classList.remove("current");
  100.   showIdx=i;
  101.   items[showIdx].classList.add("curr");
  102.   lis[showIdx].classList.add("current");
  103.   }
  104.   }
  105.   setInterval(time,3000)
  106.   </script>
  107.   </body>
  108.   </html>

  学习要多实践才可以多记住。


文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/128223.html

相关文章

  • 实现简单的播图

    摘要:小练习轮播图组件任务描述在和上一任务同一目录下面创建一个文件,在目录中创建,并在其中编码,实现一个轮播图的功能。实现思路考察对节点,定时器,事件的处理。 小练习3:轮播图组件 任务描述在和上一任务同一目录下面创建一个task0002_3.html文件,在js目录中创建task0002_3.js,并在其中编码,实现一个轮播图的功能。 图片数量及URL均在HTML中写好 可以配置轮播的顺...

    EsgynChina 评论0 收藏0
  • JS -- 记一种用原生JS 实现播图的方法(非无限循环不自动切换

    摘要:实现一个非无限循环不自动切换的轮播图只需要几张图片和两个按钮简化部分两个按钮,几张图片假如有四张图右侧按钮左侧按钮部分动态添加删除的属性部分已是最后一张图这是第一张图 实现一个非无限循环不自动切换的轮播图只需要几张图片和两个按钮(简化) HTML部分 两个按钮,几张图片(假如有四张图) 右侧按钮 左侧按钮 CSS部分 动态...

    hidogs 评论0 收藏0
  • 手把手教你用原生JavaScript造轮子(2)——播图(更新:ES6版本)

    摘要:绑定轮播事件然后是鼠标移入移出事件的绑定鼠标移入移出事件移入时停止轮播播放的定时器,移出后自动开始下一张的播放。 通过上一篇文章的学习,我们基本掌握了一个轮子的封装和开发流程。那么这次将带大家开发一个更有难度的项目——轮播图,希望能进一步加深大家对于面向对象插件开发的理解和认识。 So, Lets begin! 目前项目使用 ES5及UMD 规范封装,所以在前端暂时只支持标签的引入方式...

    jasperyang 评论0 收藏0
  • 授人以渔式解析原生JS播图

    摘要:鼠标放到轮播图的图片上时不再自动轮播并且左右箭头显示出来,鼠标移开时左右箭头隐藏掉并且自动轮播。核心原理清除定时器,绑定事件,重构下代码封装出往右往左轮播函数和自动轮播函数。 需求与分析 需求:循环无缝自动轮播五张图,按左右箭头可以手动切换图片,鼠标点击轮播图下面按钮 1 2 3 4 5会跳转到对应的第1 2 3 4 5张图片。鼠标放到轮播图的图片上时不再自动轮播并且左右箭头显示出来,...

    Zack 评论0 收藏0
  • 授人以渔式解析原生JS播图

    摘要:鼠标放到轮播图的图片上时不再自动轮播并且左右箭头显示出来,鼠标移开时左右箭头隐藏掉并且自动轮播。核心原理清除定时器,绑定事件,重构下代码封装出往右往左轮播函数和自动轮播函数。 需求与分析 需求:循环无缝自动轮播五张图,按左右箭头可以手动切换图片,鼠标点击轮播图下面按钮 1 2 3 4 5会跳转到对应的第1 2 3 4 5张图片。鼠标放到轮播图的图片上时不再自动轮播并且左右箭头显示出来,...

    SKYZACK 评论0 收藏0
  • 授人以渔式解析原生JS播图

    摘要:鼠标放到轮播图的图片上时不再自动轮播并且左右箭头显示出来,鼠标移开时左右箭头隐藏掉并且自动轮播。核心原理清除定时器,绑定事件,重构下代码封装出往右往左轮播函数和自动轮播函数。 需求与分析 需求:循环无缝自动轮播五张图,按左右箭头可以手动切换图片,鼠标点击轮播图下面按钮 1 2 3 4 5会跳转到对应的第1 2 3 4 5张图片。鼠标放到轮播图的图片上时不再自动轮播并且左右箭头显示出来,...

    Pink 评论0 收藏0

发表评论

0条评论

3403771864

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<