无缝自动轮播图(Html+Css+Js)

先上效果图

 因为录制工具的问题,效果图看起来有些卡顿,实际不会哦

实现的效果:

1.左右切换

2.自动轮播

3.鼠标移入停止自动轮播,移出继续自动轮播

4.小圆点跟随变化

5.设置了节流,降低了点击次数过快带来的瑕疵

左右按钮我使用了字体图标,自行更改哦

上代码:

<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>无缝轮播</title>
    <style>
        @font-face {
            font-family: 'icomoon';
            src: url('fonts/icomoon.eot?8b1uab');
            src: url('fonts/icomoon.eot?8b1uab#iefix') format('embedded-opentype'),
                url('fonts/icomoon.ttf?8b1uab') format('truetype'),
                url('fonts/icomoon.woff?8b1uab') format('woff'),
                url('fonts/icomoon.svg?8b1uab#icomoon') format('svg');
            font-weight: normal;
            font-style: normal;
            font-display: block;
        }

        * {
            margin: 0;
            padding: 0;
        }

        body {
            background-color: rgb(243, 245, 247);
        }

        li {
            list-style: none;
        }

        #wrap {
            position: relative;
            width: 900px;

            margin: 200px auto;
            overflow: hidden;
            border-radius: 20px;
            box-shadow: 0px 0px 10px 5px rgb(93, 93, 
  • 9
    点赞
  • 70
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
以下是使用JavaScript实现自动播放无缝轮播图的示例代码: ```html <!-- HTML结构 --> <div class="carousel"> <div class="carousel-items"> <img src="image1.jpg" alt=""> <img src="image2.jpg" alt=""> <img src="image3.jpg" alt=""> <img src="image4.jpg" alt=""> </div> </div> ``` ```css /* CSS样式 */ .carousel { position: relative; overflow: hidden; } .carousel-items { position: absolute; display: flex; width: 400%; animation: carousel 20s infinite; } .carousel-items img { width: 25%; } @keyframes carousel { 0% { left: 0; } 20% { left: 0; } 25% { left: -100%; } 45% { left: -100%; } 50% { left: -200%; } 70% { left: -200%; } 75% { left: -300%; } 95% { left: -300%; } 100% { left: -400%; } } ``` ```javascript // JavaScript代码 const carousel = document.querySelector('.carousel'); const carouselItems = document.querySelector('.carousel-items'); const images = document.querySelectorAll('.carousel-items img'); let currentIndex = 0; let timer; function startCarousel() { timer = setInterval(() => { currentIndex++; if (currentIndex > images.length - 1) { currentIndex = 0; } carouselItems.style.transform = `translateX(-${currentIndex * 25}%)`; }, 2000); } function stopCarousel() { clearInterval(timer); } carousel.addEventListener('mouseenter', stopCarousel); carousel.addEventListener('mouseleave', startCarousel); startCarousel(); ``` 解释: 1. HTML结构中,我们使用一个div元素作为轮播图的容器,内部包含一个class为carousel-items的div元素,用于存放轮播项(这里使用了4张图片作为轮播项)。 2. CSS样式中,我们设置.carousel元素为相对定位,overflow为hidden,以便隐藏.carousel-items元素的溢出部分。.carousel-items元素为绝对定位,使用flex布局,宽度为400%(即每个轮播项的宽度为25%),并使用CSS动画carousel实现轮播效果。 3. JavaScript代码中,我们首先获取.carousel、.carousel-items和.carousel-items img元素,以及当前轮播项的索引值currentIndex和定时器timer。 4. startCarousel函数用于启动轮播,使用setInterval定时器实现自动播放功能。每次定时器触发时,currentIndex加1,如果currentIndex大于轮播项的数量,则将其重置为0。然后,使用translateX属性将.carousel-items元素向左移动,以显示下一个轮播项。 5. stopCarousel函数用于停止轮播,使用clearInterval清除定时器。 6. 最后,我们为.carousel元素添加mouseenter和mouseleave事件监听器,以便在鼠标悬停在轮播图上时停止轮播,在鼠标离开轮播图时重新启动轮播。 --相关问题--: 1. 如何使用JavaScript实现手动切换轮播项? 2. 如何在轮播图中添加导航点? 3.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值