forked from surmon-china/vue-awesome-swiper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swiper.vue
78 lines (77 loc) · 1.97 KB
/
swiper.vue
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
<template>
<div class="swiper-container">
<slot name="parallax-bg"></slot>
<div :class="defaultSwiperClasses.wrapperClass">
<slot></slot>
</div>
<slot name="pagination"></slot>
<slot name="button-prev"></slot>
<slot name="button-next"></slot>
<slot name="scrollbar"></slot>
</div>
</template>
<script>
var browser = typeof window !== 'undefined'
if (browser) {
window.Swiper = require('swiper')
require('swiper/dist/css/swiper.css')
}
export default {
name: 'swiper',
props: {
options: {
type: Object,
default() {
return {
autoplay: 3500
}
}
}
},
data() {
return {
defaultSwiperClasses: {
wrapperClass: 'swiper-wrapper'
}
}
},
ready() {
if (!this.swiper && browser) {
this.swiper = new Swiper(this.$el, this.options)
}
},
mounted() {
var self = this
var mount = function() {
if (!self.swiper && browser) {
delete self.options.notNextTick
var setClassName = false
for(var className in self.defaultSwiperClasses){
if (self.defaultSwiperClasses.hasOwnProperty(className)) {
if (self.options[className]) {
setClassName = true
self.defaultSwiperClasses[className] = self.options[className]
}
}
}
var mountInstance = function() {
self.swiper = new Swiper(self.$el, self.options)
}
setClassName ? self.$nextTick(mountInstance) : mountInstance()
}
}
this.options.notNextTick ? mount() : this.$nextTick(mount)
},
updated(){
if (this.swiper) {
this.swiper.update()
}
},
beforeDestroy() {
if (this.swiper) {
this.swiper.destroy()
delete this.swiper
}
}
}
</script>