Swiper在Vue2中的使用

以swiper3为例

一、全局引入

1. 下载swiper3

cnpm install swiper@3 vue-awesome-swiper@3 --save-dev

 2. 在main.js中引入Vue-Awesome-Swiper

import VueAwesomeSwiper from 'vue-awesome-swiper'
import 'swiper/dist/css/swiper.css'
// 全局挂载
Vue.use(VueAwesomeSwiper)

 3.  在swiper.vue中 

<template>
	<div>
		<swiper :options="swiperOption" ref="mySwiper">
			<swiper-slide>I'm Slide 1</swiper-slide>
			<swiper-slide>I'm Slide 2</swiper-slide>
			<swiper-slide>I'm Slide 3</swiper-slide>
			<div class="swiper-pagination" slot="pagination"></div>
		</swiper>
	</div>
</template>

<script>
export default {
	name: 'HomeSwiper',
	data () {
		return {
			swiperOption: {
				loop: true,
				autoplay: {
					delay: 3000,
					stopOnLastSlide: false,
          			disableOnInteraction: false
				},
				pagination: {
					el: '.swiper-pagination',
                    type: 'fraction',
					clickable: true
				},
			}
		}
	}
}
</script>

<style lang="scss" scoped></style>

注意分页器的写法

2.6.7版本

swiperOption: {
    loop: true,//可选选项,开启循环
    autoplay: 5000,//可选选项,自动滑动
	pagination: '.swiper-pagination',
	paginationType: 'fraction',
	paginationClickable: true
}

3.1.3版本

swiperOption: {
    loop: true,
	autoplay: {
		delay: 3000,
		stopOnLastSlide: true, //当切换到最后一个slide时停止自动切换
        disableOnInteraction: true //用户操作swiper之后,是否禁止autoplay
	},
	pagination: {
		el: '.swiper-pagination',
		type: 'fraction',
        clickable: true
	}
}

二、按需引入

1. 下载swiper3

cnpm install swiper@3 vue-awesome-swiper@3 --save-dev

 2.  在swiper.vue中 引入样式和组件

<template>
	<div>
		<swiper :options="swiperOption" ref="mySwiper">
			<swiper-slide>I'm Slide 1</swiper-slide>
			<swiper-slide>I'm Slide 2</swiper-slide>
			<swiper-slide>I'm Slide 3</swiper-slide>
			<div class="swiper-pagination" slot="pagination"></div>
		</swiper>
	</div>
</template>

<script>
import { swiper, swiperSlide } from "vue-awesome-swiper";
import "swiper/dist/css/swiper.css";
export default {
	name: 'HomeSwiper',
	components: {
		swiper,
		swiperSlide
	},
	data () {
		return {
			swiperOption: {
				loop: true,
				autoplay: {
					delay: 3000,
					stopOnLastSlide: false,
          			disableOnInteraction: false
				},
				pagination: {
					el: '.swiper-pagination',
					clickable: true
				}
			}
		}
	}
}
</script>

<style lang="scss" scoped></style>

loop: true失效问题

数据是写死的时候,能够loop:true是有效的;

数据是动态获取的loop:true就会失效。

解决办法:

加上v-if="list.length"有效解决

computed: {
	isShowSwiper () {
		return this.list.length
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值