Vue中简单使用Swiper

本文介绍了在Vue项目中如何简单封装Swiper组件,解决Vue-Awesome-Swiper存在的问题,包括异步数据渲染导致的尺寸计算错误、swiper-wrapper额外类名设置以及事件处理。通过封装,可以更灵活地控制Swiper实例,并在需要时动态初始化和销毁。
摘要由CSDN通过智能技术生成

Swiper 是纯javascript打造的滑动特效插件,面向手机、平板电脑等移动终端。中文版文档见Swiper中文网
在应用到Vue的工程中时,有 Vue-Awesome-Swiper 可用,但是也发现一些问题,最后自己简单封装一下Swiper绕过。

先看原始版直接在Vue中使用:

<!DOCTYPE html>
<html lang="zh-CN" >
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <title>swiper demo</title>
    <link rel="dns-prefetch" href="https://cdnjs.cloudflare.com">
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.5.0/css/swiper.min.css">
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.8/vue.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.5.0/js/swiper.min.js"></script>

    <style type="text/css">
    html, body {position: relative; height: 100%; font-size: 18px; }
    body {margin: 0; padding: 0; }
    #app{height: 100%;}
    .swiper-container {width: 100%; height: 100%; }
    .swiper-slide { background: #fff; display: flex; justify-content: center; align-items: center; }
    </style>
  </head>

  <body>
    <div id="app">
        <div class="swiper-container">
            <div class="swiper-wrapper">
                <div v-for="(c, i) in bgColors" class="swiper-slide" :style="{background: c}">Slide {
  {i+1}}</div>
            </div>
            <div class="swiper-pagination"></div>
        </div>
    </div>
    <script type="text/javascript">
        var app = new Vue({
            el: '#app',
            data:{
                bgColors: ['Linen', 'Cornsilk', 'FloralWhite', 'LemonChiffon', 'MistyRose']
            },
            mounted: function(){
                this.$nextTick(function(){
                   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值