[web教学] 基于html+js实现轮播图(自动轮播、左右按钮、小圆点点击及切换图片)

[复制链接]
查看807 | 回复0 | 2023-8-23 11:46:02 | 显示全部楼层 |阅读模式 来自 中国北京
使用html和js实现的一个简单小训练轮播图。大概功能紧张是:
1、使用时间函数自动切换图片;
2、在图片及按钮及小圆点的父节点身上绑定事故署理,添加mouseenter及mouseleave事故范例,让鼠标移入,图片暂停,移出,图片规复轮播;
3、在按钮的父节点身上绑定事故署理,事故范例为click,使用event.target判断点击的目的范围,让左右两个按钮,点击可以切换上一张或下一张;
4、给图片添加样式,让下面的四个小圆点会随图片变颜色;
5、在小圆点的父节点身上绑定事故署理,事故范例为click,同样使用event.target判断点击的目的范围,让小圆点可以点击并切换到对应的图片上。
仍旧使用的是DOM2事故署理,详细的表明和代码步调我都解释在下面的代码中的,请君一阅。
【注:仅作自己检察和分享学习之用】
结果图如下:

 代码如下:
  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>轮播图</title>
  8.     <link rel="stylesheet" href="https://at.alicdn.com/t/c/font_3881267_wfv3iyzbijg.css">
  9. </head>
  10. <style>
  11.     section {
  12.         position: relative;
  13.         height: 500px;
  14.         width: 780px;
  15.         border: 1px solid;
  16.         margin: 100px auto;
  17.     }
  18.     #img {
  19.         height: 100%;
  20.         width: 100%;
  21.         background-size: 100% 100%;
  22.     }
  23.     p {
  24.         position: absolute;
  25.         left: 50%;
  26.         bottom: 0px;
  27.         transform: translate(-50%, -50%);
  28.     }
  29.     i {
  30.         height: 15px;
  31.         width: 15px;
  32.         background-color: gray;
  33.         border-radius: 50%;
  34.         display: inline-block;
  35.         margin-right: 10px;
  36.     }
  37.     i:nth-child(1) {
  38.         background-color: white;
  39.     }
  40.     i:nth-child(4) {
  41.         margin-right: 0;
  42.     }
  43.     .left,
  44.     .right {
  45.         color: rgba(255, 255, 255, 0.7);
  46.         font-size: 50px;
  47.         font-weight: bolder;
  48.         position: absolute;
  49.         top: 50%;
  50.         font-weight: 500;
  51.     }
  52.     .left {
  53.         left: 0px;
  54.         transform: translate(15%, -50%);
  55.     }
  56.     .right {
  57.         right: 0px;
  58.         transform: translate(-15%, -50%);
  59.     }
  60. </style>
  61. <body>
  62.     <section>
  63.         <span class="left iconfont icon-anniu_jiantouxiangzuo"></span>
  64.         <img src="./img/冬至竹林1.jpg" alt="" id="img">
  65.         <span class="right iconfont icon-anniu-jiantouxiangyou"></span>
  66.         <p></p>
  67.     </section>
  68.     <script src="./index.js"></script>
  69. </body>
  70. </html>
复制代码
  1. /**  需求:
  2.          *  1、自动切换图片
  3.             2、鼠标移入,图片暂停,移出,图片恢复轮播
  4.             3、左右两个按钮,点击可以切换上一张或下一张
  5.             4、下面的四个小圆点会随图片变颜色
  6.             5、小圆点可以点击并切换到对应的图片上
  7.         */
  8.         //获取把圆点节点放置的盒子节点,即p节点
  9.         let pEle = document.getElementsByTagName("p")[0];
  10.         //获取事件代理的父元素section
  11.         let secEle = document.getElementsByTagName("section")[0];
  12.         let imgArr = [
  13.             "./img/冬至竹林1.jpg",
  14.             "./img/冬至竹林2.jpg",
  15.             "./img/冬至竹林3.jpg",
  16.             "./img/冬至竹林4.jpg",
  17.         ]
  18.         //获取时间函数的起始下标
  19.         let i = 0;
  20.         //图片有多少张,就传几个参进去,并且接收这个返回的数组
  21.         let cirArr = creatCircle(imgArr.length);
  22.         //遍历cirArr数组,将圆点添加进它的父节点p节点中
  23.         cirArr.forEach(node => pEle.appendChild(node));
  24.         //获取所有的圆点节点
  25.         let iEle = document.getElementsByTagName("i");
  26.         //给每一个圆点添加上自定义属性,并赋上下标
  27.         for (let k = 0; k < iEle.length; k++) {
  28.             iEle[k].dataset.index = k;
  29.         }
  30.         let timer;
  31.         //轮播:时间函数,1秒自动换一张
  32.         function playTime() {
  33.             timer = setInterval(() => {
  34.                 //循环展示图片
  35.                 i++;
  36.                 //如果已经跳到最后一张,就再次回到第一张
  37.                 if (i > imgArr.length - 1) {
  38.                     i = 0;
  39.                 }
  40.                 //给圆点添加样式,开始运行该函数
  41.                 addStyleI(i);
  42.                 //图片标签地址(src属性)
  43.                 img.src = imgArr[i];
  44.             }, 1000);
  45.         }
  46.         playTime();
  47.         // 鼠标移入,图片暂停
  48.         secEle.addEventListener("mouseenter", function () {
  49.             clearInterval(timer);
  50.             timer = null;
  51.         });
  52.         // 鼠标移出,图片恢复滚动
  53.         secEle.addEventListener("mouseleave", playTime);
  54.         //给父节点绑定一个事件代理,点击左右按钮切换图片
  55.         secEle.addEventListener("click", function (e) {
  56.             let event = e || window.event;
  57.             //点击左右按钮切换图片
  58.             if (event.target.className == "left iconfont icon-anniu_jiantouxiangzuo") {
  59.                 i--;
  60.             }
  61.             if (event.target.className == "right iconfont icon-anniu-jiantouxiangyou") {
  62.                 i++;
  63.             }
  64.             if (i < 0) {
  65.                 i = imgArr.length - 1;
  66.             }
  67.             if (i == imgArr.length) {//3
  68.                 i = 0;
  69.             }
  70.             img.src = imgArr[i];
  71.             addStyleI(i);
  72.             //点击小圆点可以切换到对应的图片上
  73.             if (event.target.nodeName == "I") {
  74.                 console.log("11111");
  75.                 //获得点击的圆点的自定义索引值
  76.                 cirI = event.target.dataset.index;
  77.                 //替换图片
  78.                 img.src = imgArr[cirI];
  79.                 //更改圆点样式
  80.                 addStyleI(cirI);
  81.                 //每当点击小圆点,i的值就会被赋成圆点下标的值
  82.                 i = cirI;
  83.             }
  84.         });
  85.         //暂停图片滚动
  86.         function picScroll() {
  87.             clearInterval(timer);
  88.         }
  89.         //生成圆点
  90.         function creatCircle(num) {
  91.             //创建一个空数组来接收这个圆点
  92.             let iArr = [];
  93.             for (let j = 0; j < num; j++) {
  94.                 //新增圆点节点
  95.                 let circleNode = document.createElement("i");
  96.                 //再把新增的圆点节点放进圆点数组中
  97.                 iArr.push(circleNode);
  98.             }
  99.             //完成后,返回该数组
  100.             return iArr;
  101.         }
  102.         //给圆点添加样式
  103.         function addStyleI(index) {
  104.             //圆点的默认颜色是灰色
  105.             [...iEle].forEach(node => node.style.backgroundColor = "gray");
  106.             //当跳到该图片时,圆点变成白色
  107.             iEle[index].style.backgroundColor = "white";
  108.         }
复制代码


来源:https://blog.csdn.net/m0_71734538/article/details/129100213
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

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

本版积分规则