[前端] js实现轮播图自动切换

2003 0
Honkers 2022-10-21 15:41:18 | 显示全部楼层 |阅读模式
本文实例为大家分享了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.   transition: all .3s;
  23.   position: absolute;
  24.   left: 0;
  25.   /* overflow: hiddn; */
  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 lang="en">
  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 class="loopbox">
  13.   <div id="box" class="box">
  14.     <div class="box-item curr"><img src="images/1.webp"></div>
  15.     <div class="box-item"><img src="images/2.webp"></div>
  16.     <div class="box-item"><img src="images/3.webp"></div>
  17.     <div class="box-item"><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" class="page">
  22.     <li class="current"></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>
复制代码
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持中国红客联盟。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Honkers

荣誉红客

关注
  • 4010
    主题
  • 36
    粉丝
  • 0
    关注
这家伙很懒,什么都没留下!

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行