The use of Swiper in Vue2

16454019:

Take swiper3 as an example

1. Global introduction

1. Download swiper3

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

 2. Introduce Vue-Awesome-Swiper in main.js

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

 3. In 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>

Pay attention to the wording of the pager

Version 2.6.7

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

Version 3.1.3

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

2. Import on demand

1. Download swiper3

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

 2. Introduce styles and components in 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 failure problem

When the data is hard-coded, it is valid to be able to loop:true;

If the data is obtained dynamically, loop:true will be invalid.

Solution:

Add v-if="list.length" to effectively solve

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

Guess you like

Origin blog.csdn.net/iaz999/article/details/130738047