如何在 vue 中使用 swiper 插件创建轮播图
vue-awesome-swiper
大家应该都熟悉吧,在这里我就不多说了,不知道在使用的过程中有没有遇到过有些配置也不生效,在这里分享下如何使用vue-awesome-swiper
以及封装自己的swiper
组件,以满足自己的需求。
# 安装 vue-awesome-swiper
npm install vue-awesome-swiper --save-dev
1
# 全局引入vue-awesome-swiper
在 vue
的 min.js
文件中全局引入
import vueSwiper from 'vue-awesome-swiper'
import 'swiper/dist/css/swiper.css'
Vue.use(vueSwiper)
1
2
3
2
3
# 在组件中引入vue-awesome-swiper
进入自己的轮播图组件中
<template>
<swiper :options="swiperOption">
<swiper-slide class="swiper-slide" v-for="(item,index) in carouselArr" :key="index">
<img :src="item"/>
</swiper-slide>
<!-- 分页器 -->
<div class="swiper-pagination" slot="pagination"></div>
<!-- 左右箭头 -->
<div class="swiper-button-prev" slot="button-prev"></div>
<div class="swiper-button-next" slot="button-next"></div>
</swiper>
</template>
<script>
import { Swiper, SwiperSlide, directive } from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'
export default {
components: {
Swiper,
SwiperSlide
},
directives: {
swiper: directive
},
data(){
return {
swiperOption:{
//显示分页
pagination: {
el: '.swiper-pagination'
},
//设置点击箭头
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev'
},
//自动轮播
autoplay: {
delay: 2000,
//当用户滑动图片后继续自动轮播
disableOnInteraction: false,
},
//开启循环模式
loop: true
}
}
}
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 封装自己的swiper组件(推荐)
这种方式的灵活度比较大,更适合特殊场景下使用
# 1、安装下载依赖
npm install swiper --save-dev
1
# 2、挂在到 vue 上以便全局调用
import Vue from 'vue';
import Swiper from 'swiper';
import 'swiper/dist/css/swiper.css'
Vue.prototype.$swiper = Swiper;
1
2
3
4
2
3
4
# 3、进入自己的轮播图组件中
<template>
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</div>
<div class="swiper-slide">Slide 4</div>
<div class="swiper-slide">Slide 5</div>
<div class="swiper-slide">Slide 6</div>
<div class="swiper-slide">Slide 7</div>
<div class="swiper-slide">Slide 8</div>
<div class="swiper-slide">Slide 9</div>
<div class="swiper-slide">Slide 10</div>
</div>
<!-- Add Arrows -->
<div class="swiper-button-next"></div>
<div class="swiper-button-prev"></div>
<!-- Add Pagination -->
<div class="swiper-pagination"></div>
</div>
</template>
<script>
export default {
data() {
return {
swiperObj: null
};
},
props: ["content"],
watch: {
content(data) {
this.swiperInit();
}
},
mounted() {
this.swiperInit();
},
methods: {
swiperInit() {
let Swiper = this.$swiper;
this.$nextTick(() => {
let effect = 0;
let swiper = new Swiper(".swiper-container", {
loop: true,
speed: 1500,
autoplay: true,
cssMode: true,
navigation: {
nextEl: ".swiper-button-next",
prevEl: ".swiper-button-prev"
},
pagination: {
el: ".swiper-pagination"
},
mousewheel: true,
keyboard: true
});
let comtainer = document.querySelector(".swiper-container");
comtainer.onmouseenter = () => {
swiper.autoplay.stop();
};
comtainer.onmouseleave = () => {
swiper.autoplay.start();
};
});
}
}
};
</script>
<style lang="scss" scoped>
.swiper-container {
width: 100%;
height: 100%;
}
.swiper-slide {
text-align: center;
font-size: 18px;
background: #fff;
display: flex;
justify-content: center;
align-items: center;
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
上次更新: 2024/01/30, 00:35:17
- 02
- Node与GLIBC_2.27不兼容解决方案08-19
- 03
- Git清空本地文件跟踪缓存08-13