前端小案例——轮播图(HTML+CSS+JS, 附源码)

打印 上一主题 下一主题

主题 972|帖子 972|积分 2916

一、前言

实现功能:



  • 表现多张图片,每张图片占据轮播图容器的一部分空间。
  • 实现向左和向右的切换按钮,可以点击按钮切换到上一张或下一张图片。
  • 在底部表现小圆点,表现当前表现的图片,点击小圆点可以跳转到对应的图片。
实现逻辑:


  • 起首,利用querySelectorAll方法获取所有的轮播项元素,并将其生存在items变量中。
  • 定义一个全局变量current,用于记录当前表现的轮播项的索引,默认为0。通过操作current变量来控制表现的轮播项。并通过调用干系函数来更新表现和样式
  • 定义showSlide函数,用于表现当前的轮播项。通过遍历所有轮播项,设置其transform属性来实现水平滑动效果。同时,调用updateDots函数更新小圆点的样式。
  • 定义prevSlide函数,用于切换到上一张轮播项。如果current大于0,则将current减1;否则,将current设置为末了一张轮播项的索引。然后,调用showSlide函数表现对应的轮播项。
  • 定义nextSlide函数,用于切换到下一张轮播项。如果current小于轮播项的数量减1,则将current加1;否则,将current设置为0。然后,调用showSlide函数表现对应的轮播项。
  • 利用setInterval方法定义一个定时器timer,每隔3秒主动调用一次nextSlide函数,实现主动播放功能。
  • 定义pauseTimer函数,用于停息定时器。通过调用clearInterval方法,扫除定时器timer。
  • 定义resumeTimer函数,用于恢复定时器。重新设置定时器timer,调用nextSlide函数实现轮播功能。
  • 添加鼠标悬停和离开事件监听器,当鼠标悬停在轮播图上时,调用pauseTimer函数停息主动播放;当鼠标离开时,调用resumeTimer函数恢复主动播放。
  • 利用querySelectorAll方法获取所有的小圆点元素,并将其生存在dots变量中。
  • 定义updateDots函数,用于更新小圆点的样式。遍历所有小圆点元素,移除它们的active类名,然后根据current变量为当前轮播项对应的小圆点添加active类名。
  • 定义jumpToSlide函数,用于跳转到指定的轮播项。将current变量设置为指定轮播项的索引,然后调用showSlide和updateDots函数,表现对应的轮播项并更新小圆点的样式。
二、项目运行效果 


三、全部代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>轮播图</title>
  7.     <style>
  8.         *{
  9.             margin: 0;
  10.             padding: 0;
  11.             text-decoration: none;
  12.             list-style: none;
  13.             background-repeat: no-repeat;
  14.         }
  15.         .carousel {
  16.             position: relative;
  17.             width: 100%;
  18.             overflow: hidden;
  19.         }
  20.         .carousel-inner {
  21.             display: flex;
  22.             width: 100%;
  23.             transition: transform 0.6s ease-in-out;
  24.         }
  25.         .item {
  26.             flex: 0 0 100%;
  27.             height: 55vh;
  28.         }
  29.         .item img {
  30.             width: 100%;
  31.             height: 100%;
  32.             object-fit: cover;
  33.         }
  34.         .carousel-control {
  35.             position: absolute;
  36.             top: 50%;
  37.             transform: translateY(-50%);
  38.             color: #fff;
  39.             font-size: 80px;
  40.             z-index: 10;
  41.             cursor: pointer;
  42.         }
  43.         .left {
  44.             left: 25px;
  45.         }
  46.         .right {
  47.             right: 25px;
  48.         }
  49.         .dots {
  50.             position: absolute;
  51.             bottom: 20px;
  52.             left: 50%;
  53.             z-index: 15;
  54.             width: 60%;
  55.             padding-left: 0;
  56.             margin-left: -30%;
  57.             text-align: center;
  58.             list-style: none;
  59.         }
  60.         .dots > li {
  61.             display: inline-block;
  62.             width: 10px;
  63.             height: 10px;
  64.             margin: 1px;
  65.             cursor: pointer;
  66.             background-color: rgba(0,0,0,0);
  67.             border: 1px solid #fff;
  68.             border-radius: 10px;
  69.         }
  70.         .dots .active {
  71.             width: 12px;
  72.             height: 12px;
  73.             margin: 0;
  74.             background-color: #fff;
  75.         }
  76.     </style>
  77. </head>
  78. <body>
  79.     <div class="carousel" id="carousel">
  80.         <div class="carousel-inner">
  81.             <div class="item">
  82.                 <img src="1.png" style="background-color: pink;">
  83.             </div>
  84.             <div class="item">
  85.                 <img src="2.png" style="background-color: bisque;">
  86.             </div>
  87.             <div class="item">
  88.                 <img src="3.jpg" style="background-color: rgb(144, 255, 236);">
  89.             </div>
  90.             <div class="item">
  91.                 <img src="4.jpg" style="background-color: rgb(248, 99, 124);">
  92.             </div>
  93.             <div class="item">
  94.                 <img src="5.jpg" style="background-color: rgb(210, 161, 250);">
  95.             </div>
  96.         </div>
  97.         <div class="carousel-control left" onclick="prevSlide()">&lsaquo;</div>
  98.         <div class="carousel-control right" onclick="nextSlide()">&rsaquo;</div>
  99.         <div class="dots">
  100.             <li class="active" onclick="jumpToSlide(0)"></li>
  101.             <li onclick="jumpToSlide(1)"></li>
  102.             <li onclick="jumpToSlide(2)"></li>
  103.             <li onclick="jumpToSlide(3)"></li>
  104.             <li onclick="jumpToSlide(4)"></li>
  105.         </div>
  106.     </div>
  107. </body>
  108.     <script>
  109.         let items = document.querySelectorAll('.item');
  110.         let current = 0;
  111.         function showSlide() {
  112.             items.forEach(item => {
  113.                 item.style.transform = `translateX(-${current * 100}%)`;
  114.             });
  115.             updateDots();
  116.         }
  117.         function prevSlide() {
  118.             if (current > 0) {
  119.                 current--;
  120.             } else {
  121.                 current = items.length - 1;
  122.             }
  123.             showSlide();
  124.         }
  125.         function nextSlide() {
  126.             if (current < items.length - 1) {
  127.                 current++;
  128.             } else {
  129.                 current = 0;
  130.             }
  131.             showSlide();
  132.         }
  133.         let timer = setInterval(nextSlide, 3000);
  134.         function pauseTimer() {
  135.             clearInterval(timer);
  136.         }
  137.         function resumeTimer() {
  138.             timer = setInterval(nextSlide, 3000);
  139.         }
  140.         document.getElementById('carousel').addEventListener('mouseover', pauseTimer);
  141.         document.getElementById('carousel').addEventListener('mouseout', resumeTimer);
  142.         let dots = document.querySelectorAll('.dots li');
  143.         function updateDots() {
  144.             dots.forEach(dot => {
  145.                 dot.classList.remove('active');
  146.             });
  147.             dots[current].classList.add('active');
  148.         }
  149.         function jumpToSlide(index) {
  150.             current = index;
  151.             showSlide();
  152.             updateDots();
  153.         }
  154.     </script>
  155. </html>
复制代码
四、答疑解惑

        这是一个非常适合前端入门练习的小案例,各位小伙伴可以自行更改样式和图片,如果大家运行时出现问题或代码有什么不懂的地方都可以随时评论留言或联系博主。
        还多请各位小伙伴多多点赞支持,你们的支持是我最大的动力。
博主VX:18884281851

谢谢各位的支持~~


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

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

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

写过一篇

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表