swiper.js实现叠加轮播效果

@swiper叠加轮播图

需求

  1. 堆叠展示,默认展示数据中间位置的数据
  2. 可进行翻页展示(前进/后退)

效果图

在这里插入图片描述

demo源码

<html lang="en">
<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>swiper叠加轮播图</title>
</head>
<body>
    <link rel="stylesheet" href="https://unpkg.com/swiper@7/swiper-bundle.min.css" />
    <style>
        body {
            display: flex;
            align-items: center;
            justify-content: center;
        }
        /* 外层容器 */
        .swiper {
            width: 1310px;
            height: 482px;
        }
        .swiper-wrapper {
            width: 1216px;
            height: 482px;
        }
        /* 轮播项 */
        .swiper .swiper-slide {
            width: 387px;
            height: 482px;
            font-size: 32px;
            font-weight: 500;
            color: #ffffff;
            /* 文字垂直居中 */
            display: flex;
            justify-content: center;
            align-items: center;
        }
        /* 色彩来源:https://color.uisdc.com/ */
        .swiper .swiper-slide-1 {
            background-color: #425066;
        }
        .swiper .swiper-slide-2 {
            background-color: #b88ed6;
        }
        .swiper .swiper-slide-3 {
            background-color: #9d2933;
        }
        .swiper .swiper-slide-4 {
            background-color: #003371;
        }
        .swiper .swiper-slide-5 {
            background-color: #4c8dae;
        }
        .swiper .swiper-slide-6 {
            background-color: #72db09;
        }
        .swiper .swiper-slide-7 {
            background-color: #f72905;
        }
        .swiper .swiper-slide-8 {
            background-color: #03a8fb;
        }
        .swiper .swiper-slide-9 {
            background-color: #9803fb;
        }
        /* .swiper-button-prev {
            width: 34px;
            height: 34px;
            background: url("./img/prev.png") no-repeat;
            background-size: 100% 100%;
        }
        .swiper-button-next {
            width: 34px;
            height: 34px;
            background: url("./img/next.png") no-repeat;
            background-size: 100% 100%;
        }
         去除默认的操作按钮样式
        .swiper-button-next:after,
        .swiper-button-prev:after {
            display: none;
        } */
    </style>
    <div class="swiper">
        <div class="swiper-wrapper">
            <div class="swiper-slide swiper-slide-1">1</div>
            <div class="swiper-slide swiper-slide-2">2</div>
            <div class="swiper-slide swiper-slide-3">3</div>
            <div class="swiper-slide swiper-slide-4">4</div>
            <div class="swiper-slide swiper-slide-5">5</div>
            <div class="swiper-slide swiper-slide-6">6</div>
            <div class="swiper-slide swiper-slide-7">7</div>
            <div class="swiper-slide swiper-slide-8">8</div>
            <div class="swiper-slide swiper-slide-9">9</div>
        </div>
        <div class="swiper-button-prev"></div>
        <div class="swiper-button-next"></div>
    </div>
    <script src="https://unpkg.com/swiper@7/swiper-bundle.min.js"></script>
    <script>
        const swiper = new Swiper('.swiper', {
            initialSlide: Math.floor(9 / 2), //默认第几个在中心位置显示,总数量中间的一个,计算
            autoplay: true,
            // 容器能够显示的slide数量,可以用来调节中间slide占比大小(为了达到设计图的尺寸),
            // slide不能固定宽度,会影响滑动效果,所以这里用比例调节
            slidesPerView: "auto", // 每个slide的宽度:外层容器 1310 / slidesPerView = 387
            // 当前active的slide居中-最前面的大图
            centeredSlides: true,
            effect: "creative",
            loop: true,
            grabCursor: true,
            creativeEffect: {
                prev: {
                    translate: [-115, 0, 0], // 偏移量
                    scale: 0.85, // 缩放量
                    shadow: true, // 是否加阴影
                },
                next: {
                    translate: [140, 0, 0],
                    scale: 0.85,
                    shadow: true,
                },
                limitProgress: Math.floor(9 / 2), // 显示五个堆叠的最重要的这个属性,后面依次以前面属性等比配置
                shadowPerProgress: true, //是否等比配置透明度
            },
            navigation: {
                nextEl: '.swiper-button-next',
                prevEl: '.swiper-button-prev',
            },
        });
        // 监听改变事件
        swiper.on('slideChange', (e) => {
            console.log(e.realIndex);
        });
    </script>
</body>
</html>

推荐一个比较好使得插件 Vue 3D轮播插件vue-carousel-3d
参考地址:参考地址

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用 swiper.css 实现的代码示例: 首先,我们需要在 HTML 中引入 Swiper 的 CSS 和 JS 文件。可以在 head 标签中添加以下代码: ```html <head> <!-- 引入 Swiper CSS 文件 --> <link rel="stylesheet" href="https://unpkg.com/swiper/css/swiper.min.css"> </head> ``` 然后,在 body 标签中添加轮图的 HTML 代码: ```html <div class="swiper-container"> <div class="swiper-wrapper"> <div class="swiper-slide"><img src="image1.jpg"></div> <div class="swiper-slide"><img src="image2.jpg"></div> <div class="swiper-slide"><img src="image3.jpg"></div> </div> <div class="swiper-pagination"></div> </div> ``` 其中,swiper-container 是容器,swiper-wrapper 是轮图的包裹层,swiper-slide 是每一张图片的容器,swiper-pagination 是分页器。 最后,在 body 标签底部添加以下代码: ```html <body> <!-- 引入 Swiper JS 文件 --> <script src="https://unpkg.com/swiper/js/swiper.min.js"></script> <!-- 初始化 Swiper --> <script> var mySwiper = new Swiper('.swiper-container', { autoplay: true, // 自动轮 loop: true, // 循环轮 pagination: { el: '.swiper-pagination', // 分页器元素 clickable: true, // 可点击分页器切换 }, }); </script> </body> ``` 以上代码中,我们通过 new Swiper() 方法来初始化 Swiper图。其中,autoplay 表示自动轮,loop 表示循环轮,pagination 表示分页器。 最后,我们就可以通过 swiper.css 实现了。希望对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值