vue-swiper组件

本文介绍如何在Vue中自定义封装Swiper组件,解决初始化过早的问题,并演示了使用Swiper进行轮播图展示的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自定义封装swiper组件(基于swiper) 
注意: 防止swipe初始化过早
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Examples</title>
    <script type="text/javascript" src="lib/vue.js"></script>
    <link rel="stylesheet" href="lib/swiper/css/swiper.css">
    <script src="lib/swiper/js/swiper.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <style>
        .swiper-container {
            width: 600px;
            height: 300px;
        }
        .swiper-slide img {
            width: 100%;
        }
    </style>
</head>
<body>
    <div id="box">
        <swiper :key="datalist.length">
            <div class="swiper-slide" v-for="data in datalist">
                {{data}}
            </div>
        </swiper>
    </div>

    <script type="text/javascript">
        Vue.component("swiper", {
            template: ` <div :class="'swiper-container ' + which">
            <div class="swiper-wrapper">
                <slot></slot>
            </div>
            <!-- 如果需要分页器 -->
            <div class="swiper-pagination"></div>
          </div>`,
            data() {
                return {
                    which: "a"
                }
            },

            mounted() {
                console.log("swiper-mounted")
                new Swiper('.a', {
                    // direction: 'vertical'
                    loop: true,
                    // 如果需要分页器
                    pagination: {
                        el: '.swiper-pagination',
                    }
                })
            }
        })
        new Vue({
            el: "#box",
            data: {
                datalist: [],
                // which:"a"
            },
            mounted() {
                console.log("root-mounted")
                setTimeout(() => {
                    this.datalist = ["1111", "22222", "33333"]
                }, 2000)
            },
        })
      //  obox.className = "swiper-contrian "+which
    </script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值