6c9acb08
Rijn
initialize carousel
|
1
|
<template>
|
41f83010
Rijn
update props and ...
|
2
|
<div :class="classes">
|
e9989f2b
Rijn
added horizontal ...
|
3
4
5
|
<button :class="arrowClasses" class="left" @click="add(-1)">
<Icon type="chevron-left"></Icon>
</button>
|
65c64ce9
Rijn
carousel basic po...
|
6
7
|
<div :class="[prefixCls + '-list']">
<div :class="[prefixCls + '-track']" :style="trackStyles" v-el:slides>
|
65c64ce9
Rijn
carousel basic po...
|
8
9
10
|
<slot></slot>
</div>
</div>
|
e9989f2b
Rijn
added horizontal ...
|
11
12
13
14
15
16
17
18
19
20
21
22
|
<button :class="arrowClasses" class="right" @click="add(1)">
<Icon type="chevron-right"></Icon>
</button>
<ul :class="dotsClasses">
<template v-for="n in slides.length">
<li :class="{ [`${prefixCls}-active`]: n === currentIndex }"
@click="dotsEvent('click', n)"
@mouseover="dotsEvent('hover', n)">
<button></button>
</li>
</template>
</ul>
|
6c9acb08
Rijn
initialize carousel
|
23
24
25
|
</div>
</template>
<script>
|
41f83010
Rijn
update props and ...
|
26
|
import Icon from '../icon/icon.vue';
|
50f4ac70
Rijn
Merge branch 'mas...
|
27
|
import { getStyle } from '../../utils/assist';
|
41f83010
Rijn
update props and ...
|
28
29
|
const prefixCls = 'ivu-carousel';
|
6c9acb08
Rijn
initialize carousel
|
30
31
|
export default {
|
41f83010
Rijn
update props and ...
|
32
|
name: 'Carousel',
|
50f4ac70
Rijn
Merge branch 'mas...
|
33
|
components: { Icon },
|
41f83010
Rijn
update props and ...
|
34
|
props: {
|
e9989f2b
Rijn
added horizontal ...
|
35
36
37
|
arrow: {
type: String,
default: 'hover'
|
41f83010
Rijn
update props and ...
|
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
},
autoplay: {
type: Boolean,
default: true
},
autoplaySpeed: {
type: Number,
default: 2000
},
easing: {
type: String,
default: 'ease'
},
dots: {
|
e9989f2b
Rijn
added horizontal ...
|
52
53
54
55
56
57
|
type: String,
default: 'inside'
},
trigger: {
type: String,
default: 'click'
|
41f83010
Rijn
update props and ...
|
58
|
},
|
3e6d6356
Rijn
implement basic t...
|
59
60
61
|
currentIndex: {
type: Number,
default: 0
|
9cd69375
Rijn
added height props
|
62
63
64
65
|
},
height: {
type: [String, Number],
default: 'auto'
|
41f83010
Rijn
update props and ...
|
66
67
|
}
},
|
65c64ce9
Rijn
carousel basic po...
|
68
69
70
71
72
|
data () {
return {
prefixCls: prefixCls,
listWidth: 0,
trackWidth: 0,
|
9cd69375
Rijn
added height props
|
73
|
trackOffset: 0,
|
65c64ce9
Rijn
carousel basic po...
|
74
|
slides: [],
|
3e6d6356
Rijn
implement basic t...
|
75
|
slideInstances: [],
|
9cd69375
Rijn
added height props
|
76
77
|
timer: null,
ready: false
|
50f4ac70
Rijn
Merge branch 'mas...
|
78
|
};
|
65c64ce9
Rijn
carousel basic po...
|
79
|
},
|
41f83010
Rijn
update props and ...
|
80
81
82
|
computed: {
classes () {
return [
|
9cd69375
Rijn
added height props
|
83
|
`${prefixCls}`
|
41f83010
Rijn
update props and ...
|
84
|
];
|
65c64ce9
Rijn
carousel basic po...
|
85
86
87
|
},
trackStyles () {
return {
|
3e6d6356
Rijn
implement basic t...
|
88
|
width: `${this.trackWidth}px`,
|
9cd69375
Rijn
added height props
|
89
|
transform: `translate3d(-${this.trackOffset}px, 0px, 0px)`,
|
3e6d6356
Rijn
implement basic t...
|
90
|
transition: `transform 500ms ${this.easing}`
|
65c64ce9
Rijn
carousel basic po...
|
91
|
};
|
e9989f2b
Rijn
added horizontal ...
|
92
93
94
95
96
|
},
arrowClasses () {
return [
`${prefixCls}-arrow`,
`${prefixCls}-arrow-${this.arrow}`
|
50f4ac70
Rijn
Merge branch 'mas...
|
97
|
];
|
e9989f2b
Rijn
added horizontal ...
|
98
99
100
101
|
},
dotsClasses () {
return [
`${prefixCls}-dots`,
|
8738f4d3
Rijn
added outside dots
|
102
|
`${prefixCls}-dots-${this.dots}`
|
50f4ac70
Rijn
Merge branch 'mas...
|
103
|
];
|
65c64ce9
Rijn
carousel basic po...
|
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
}
},
methods: {
// find option component
findChild (cb) {
const find = function (child) {
const name = child.$options.componentName;
if (name) {
cb(child);
} else if (child.$children.length) {
child.$children.forEach((innerChild) => {
find(innerChild, cb);
});
}
};
|
9cd69375
Rijn
added height props
|
121
|
if (this.slideInstances.length || !this.$children) {
|
65c64ce9
Rijn
carousel basic po...
|
122
123
124
125
126
127
128
129
130
|
this.slideInstances.forEach((child) => {
find(child);
});
} else {
this.$children.forEach((child) => {
find(child);
});
}
},
|
50f4ac70
Rijn
Merge branch 'mas...
|
131
|
updateSlides (init ) {
|
65c64ce9
Rijn
carousel basic po...
|
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
let slides = [];
let index = 1;
this.findChild((child) => {
slides.push({
$el: child.$el
});
child.index = index++;
if (init) {
this.slideInstances.push(child);
}
});
this.slides = slides;
|
8f4e2cf0
Rijn
update pos when s...
|
147
148
|
this.updatePos();
|
65c64ce9
Rijn
carousel basic po...
|
149
150
151
152
|
},
updatePos () {
this.findChild((child) => {
child.width = this.listWidth;
|
9cd69375
Rijn
added height props
|
153
|
child.height = typeof this.height === 'number' ? `${this.height}px` : this.height;
|
65c64ce9
Rijn
carousel basic po...
|
154
155
156
157
158
159
|
});
this.trackWidth = (this.slides.length || 0) * this.listWidth;
},
// use when slot changed
slotChange () {
|
373dfb3c
Rijn
removed observer
|
160
161
162
163
164
165
166
|
this.$nextTick(() => {
this.slides = [];
this.slideInstances = [];
this.updateSlides(true, true);
this.updatePos();
});
|
65c64ce9
Rijn
carousel basic po...
|
167
168
|
},
handleResize () {
|
9cd69375
Rijn
added height props
|
169
170
|
this.listWidth = parseInt(getStyle(this.$el, 'width'));
this.updatePos();
|
3e6d6356
Rijn
implement basic t...
|
171
|
},
|
77d460e8
Rijn
added offset func...
|
172
173
174
|
add (offset) {
let index = this.currentIndex;
index += offset;
|
5e8be9b3
Rijn
fixed add bug. ad...
|
175
176
|
while (index < 0) index += this.slides.length;
index = index % this.slides.length;
|
77d460e8
Rijn
added offset func...
|
177
178
|
this.currentIndex = index;
},
|
e9989f2b
Rijn
added horizontal ...
|
179
|
dotsEvent (event, n) {
|
c1af3fac
Rijn
added on-change e...
|
180
|
if (event === this.trigger && this.currentIndex !== n) {
|
e9989f2b
Rijn
added horizontal ...
|
181
182
|
this.currentIndex = n;
}
|
bfc11079
Rijn
updated autoplay ...
|
183
|
},
|
3e6d6356
Rijn
implement basic t...
|
184
|
setAutoplay () {
|
bfc11079
Rijn
updated autoplay ...
|
185
|
window.clearInterval(this.timer);
|
3e6d6356
Rijn
implement basic t...
|
186
187
|
if (this.autoplay) {
this.timer = window.setInterval(() => {
|
77d460e8
Rijn
added offset func...
|
188
|
this.add(1);
|
3e6d6356
Rijn
implement basic t...
|
189
|
}, this.autoplaySpeed);
|
3e6d6356
Rijn
implement basic t...
|
190
|
}
|
9cd69375
Rijn
added height props
|
191
192
193
194
195
196
|
},
updateOffset () {
this.$nextTick(() => {
this.handleResize();
this.trackOffset = this.currentIndex * this.listWidth;
});
|
41f83010
Rijn
update props and ...
|
197
|
}
|
65c64ce9
Rijn
carousel basic po...
|
198
199
200
|
},
compiled () {
this.updateSlides(true);
|
65c64ce9
Rijn
carousel basic po...
|
201
|
},
|
3e6d6356
Rijn
implement basic t...
|
202
203
204
205
|
watch: {
autoplay () {
this.setAutoplay();
},
|
bfc11079
Rijn
updated autoplay ...
|
206
207
208
|
autoplaySpeed () {
this.setAutoplay();
},
|
c1af3fac
Rijn
added on-change e...
|
209
|
currentIndex (val, oldVal) {
|
50f4ac70
Rijn
Merge branch 'mas...
|
210
|
this.$emit('on-change', oldVal, val);
|
9cd69375
Rijn
added height props
|
211
212
213
214
|
this.updateOffset();
},
height () {
this.updatePos();
|
3e6d6356
Rijn
implement basic t...
|
215
216
|
}
},
|
65c64ce9
Rijn
carousel basic po...
|
217
218
|
ready () {
this.handleResize();
|
bfc11079
Rijn
updated autoplay ...
|
219
|
this.setAutoplay();
|
65c64ce9
Rijn
carousel basic po...
|
220
221
222
223
|
window.addEventListener('resize', this.handleResize, false);
},
beforeDestroy () {
window.removeEventListener('resize', this.handleResize, false);
|
41f83010
Rijn
update props and ...
|
224
|
}
|
6c9acb08
Rijn
initialize carousel
|
225
226
|
};
</script>
|