6c9acb08
Rijn
initialize carousel
|
1
|
<template>
|
41f83010
Rijn
update props and ...
|
2
|
<div :class="classes">
|
932db623
Rijn
remove autoplay d...
|
3
|
<button :class="arrowClasses" class="left" @click="arrowEvent(-1)">
|
e9989f2b
Rijn
added horizontal ...
|
4
5
|
<Icon type="chevron-left"></Icon>
</button>
|
65c64ce9
Rijn
carousel basic po...
|
6
|
<div :class="[prefixCls + '-list']">
|
bb71140e
梁灏
support Carousel
|
7
|
<div :class="[prefixCls + '-track']" :style="trackStyles">
|
65c64ce9
Rijn
carousel basic po...
|
8
9
10
|
<slot></slot>
</div>
</div>
|
932db623
Rijn
remove autoplay d...
|
11
|
<button :class="arrowClasses" class="right" @click="arrowEvent(1)">
|
e9989f2b
Rijn
added horizontal ...
|
12
13
14
15
|
<Icon type="chevron-right"></Icon>
</button>
<ul :class="dotsClasses">
<template v-for="n in slides.length">
|
bb71140e
梁灏
support Carousel
|
16
17
18
|
<li :class="[n - 1 === currentIndex ? prefixCls + '-active' : '']"
@click="dotsEvent('click', n - 1)"
@mouseover="dotsEvent('hover', n - 1)">
|
e9989f2b
Rijn
added horizontal ...
|
19
20
21
22
|
<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';
|
53e38f9c
Rijn
added prop valida...
|
27
|
import { getStyle, oneOf } 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
|
arrow: {
type: String,
|
53e38f9c
Rijn
added prop valida...
|
37
38
39
40
|
default: 'hover',
validator (value) {
return oneOf(value, ['hover', 'always', 'never']);
}
|
41f83010
Rijn
update props and ...
|
41
42
43
|
},
autoplay: {
type: Boolean,
|
5139233b
梁灏
update Carousel
|
44
|
default: false
|
41f83010
Rijn
update props and ...
|
45
46
47
48
49
|
},
autoplaySpeed: {
type: Number,
default: 2000
},
|
41f83010
Rijn
update props and ...
|
50
51
52
53
54
|
easing: {
type: String,
default: 'ease'
},
dots: {
|
e9989f2b
Rijn
added horizontal ...
|
55
|
type: String,
|
53e38f9c
Rijn
added prop valida...
|
56
57
58
59
|
default: 'inside',
validator (value) {
return oneOf(value, ['inside', 'outside', 'none']);
}
|
e9989f2b
Rijn
added horizontal ...
|
60
61
62
|
},
trigger: {
type: String,
|
53e38f9c
Rijn
added prop valida...
|
63
64
65
66
|
default: 'click',
validator (value) {
return oneOf(value, ['click', 'hover']);
}
|
41f83010
Rijn
update props and ...
|
67
|
},
|
bb71140e
梁灏
support Carousel
|
68
|
value: {
|
3e6d6356
Rijn
implement basic t...
|
69
70
|
type: Number,
default: 0
|
9cd69375
Rijn
added height props
|
71
72
73
|
},
height: {
type: [String, Number],
|
53e38f9c
Rijn
added prop valida...
|
74
75
|
default: 'auto',
validator (value) {
|
0b1b650d
Rijn
fix #269.
|
76
|
return value === 'auto' || Object.prototype.toString.call(value) === '[object Number]';
|
53e38f9c
Rijn
added prop valida...
|
77
|
}
|
41f83010
Rijn
update props and ...
|
78
79
|
}
},
|
65c64ce9
Rijn
carousel basic po...
|
80
81
82
83
84
|
data () {
return {
prefixCls: prefixCls,
listWidth: 0,
trackWidth: 0,
|
9cd69375
Rijn
added height props
|
85
|
trackOffset: 0,
|
65c64ce9
Rijn
carousel basic po...
|
86
|
slides: [],
|
3e6d6356
Rijn
implement basic t...
|
87
|
slideInstances: [],
|
9cd69375
Rijn
added height props
|
88
|
timer: null,
|
bb71140e
梁灏
support Carousel
|
89
90
|
ready: false,
currentIndex: this.value
|
50f4ac70
Rijn
Merge branch 'mas...
|
91
|
};
|
65c64ce9
Rijn
carousel basic po...
|
92
|
},
|
41f83010
Rijn
update props and ...
|
93
94
95
|
computed: {
classes () {
return [
|
9cd69375
Rijn
added height props
|
96
|
`${prefixCls}`
|
41f83010
Rijn
update props and ...
|
97
|
];
|
65c64ce9
Rijn
carousel basic po...
|
98
99
100
|
},
trackStyles () {
return {
|
3e6d6356
Rijn
implement basic t...
|
101
|
width: `${this.trackWidth}px`,
|
9cd69375
Rijn
added height props
|
102
|
transform: `translate3d(-${this.trackOffset}px, 0px, 0px)`,
|
3e6d6356
Rijn
implement basic t...
|
103
|
transition: `transform 500ms ${this.easing}`
|
65c64ce9
Rijn
carousel basic po...
|
104
|
};
|
e9989f2b
Rijn
added horizontal ...
|
105
106
107
108
109
|
},
arrowClasses () {
return [
`${prefixCls}-arrow`,
`${prefixCls}-arrow-${this.arrow}`
|
50f4ac70
Rijn
Merge branch 'mas...
|
110
|
];
|
e9989f2b
Rijn
added horizontal ...
|
111
112
113
114
|
},
dotsClasses () {
return [
`${prefixCls}-dots`,
|
8738f4d3
Rijn
added outside dots
|
115
|
`${prefixCls}-dots-${this.dots}`
|
50f4ac70
Rijn
Merge branch 'mas...
|
116
|
];
|
65c64ce9
Rijn
carousel basic po...
|
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
}
},
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
|
134
|
if (this.slideInstances.length || !this.$children) {
|
65c64ce9
Rijn
carousel basic po...
|
135
136
137
138
139
140
141
142
143
|
this.slideInstances.forEach((child) => {
find(child);
});
} else {
this.$children.forEach((child) => {
find(child);
});
}
},
|
bb71140e
梁灏
support Carousel
|
144
|
updateSlides (init) {
|
65c64ce9
Rijn
carousel basic po...
|
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
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...
|
160
161
|
this.updatePos();
|
65c64ce9
Rijn
carousel basic po...
|
162
163
164
165
|
},
updatePos () {
this.findChild((child) => {
child.width = this.listWidth;
|
9cd69375
Rijn
added height props
|
166
|
child.height = typeof this.height === 'number' ? `${this.height}px` : this.height;
|
65c64ce9
Rijn
carousel basic po...
|
167
168
169
170
171
172
|
});
this.trackWidth = (this.slides.length || 0) * this.listWidth;
},
// use when slot changed
slotChange () {
|
373dfb3c
Rijn
removed observer
|
173
174
175
176
177
178
|
this.$nextTick(() => {
this.slides = [];
this.slideInstances = [];
this.updateSlides(true, true);
this.updatePos();
|
62808b2b
Rijn
reset autoplay ti...
|
179
|
this.updateOffset();
|
373dfb3c
Rijn
removed observer
|
180
|
});
|
65c64ce9
Rijn
carousel basic po...
|
181
182
|
},
handleResize () {
|
9cd69375
Rijn
added height props
|
183
184
|
this.listWidth = parseInt(getStyle(this.$el, 'width'));
this.updatePos();
|
62808b2b
Rijn
reset autoplay ti...
|
185
|
this.updateOffset();
|
3e6d6356
Rijn
implement basic t...
|
186
|
},
|
77d460e8
Rijn
added offset func...
|
187
188
189
|
add (offset) {
let index = this.currentIndex;
index += offset;
|
5e8be9b3
Rijn
fixed add bug. ad...
|
190
191
|
while (index < 0) index += this.slides.length;
index = index % this.slides.length;
|
77d460e8
Rijn
added offset func...
|
192
|
this.currentIndex = index;
|
bb71140e
梁灏
support Carousel
|
193
|
this.$emit('input', index);
|
77d460e8
Rijn
added offset func...
|
194
|
},
|
932db623
Rijn
remove autoplay d...
|
195
196
197
198
|
arrowEvent (offset) {
this.setAutoplay();
this.add(offset);
},
|
e9989f2b
Rijn
added horizontal ...
|
199
|
dotsEvent (event, n) {
|
c1af3fac
Rijn
added on-change e...
|
200
|
if (event === this.trigger && this.currentIndex !== n) {
|
e9989f2b
Rijn
added horizontal ...
|
201
|
this.currentIndex = n;
|
bb71140e
梁灏
support Carousel
|
202
|
this.$emit('input', n);
|
62808b2b
Rijn
reset autoplay ti...
|
203
204
|
// Reset autoplay timer when trigger be activated
this.setAutoplay();
|
e9989f2b
Rijn
added horizontal ...
|
205
|
}
|
bfc11079
Rijn
updated autoplay ...
|
206
|
},
|
3e6d6356
Rijn
implement basic t...
|
207
|
setAutoplay () {
|
bfc11079
Rijn
updated autoplay ...
|
208
|
window.clearInterval(this.timer);
|
3e6d6356
Rijn
implement basic t...
|
209
210
|
if (this.autoplay) {
this.timer = window.setInterval(() => {
|
932db623
Rijn
remove autoplay d...
|
211
|
this.add(1);
|
3e6d6356
Rijn
implement basic t...
|
212
|
}, this.autoplaySpeed);
|
3e6d6356
Rijn
implement basic t...
|
213
|
}
|
9cd69375
Rijn
added height props
|
214
215
216
|
},
updateOffset () {
this.$nextTick(() => {
|
9cd69375
Rijn
added height props
|
217
218
|
this.trackOffset = this.currentIndex * this.listWidth;
});
|
41f83010
Rijn
update props and ...
|
219
|
}
|
65c64ce9
Rijn
carousel basic po...
|
220
|
},
|
3e6d6356
Rijn
implement basic t...
|
221
222
223
224
|
watch: {
autoplay () {
this.setAutoplay();
},
|
bfc11079
Rijn
updated autoplay ...
|
225
226
227
|
autoplaySpeed () {
this.setAutoplay();
},
|
c1af3fac
Rijn
added on-change e...
|
228
|
currentIndex (val, oldVal) {
|
50f4ac70
Rijn
Merge branch 'mas...
|
229
|
this.$emit('on-change', oldVal, val);
|
9cd69375
Rijn
added height props
|
230
231
232
233
|
this.updateOffset();
},
height () {
this.updatePos();
|
bb71140e
梁灏
support Carousel
|
234
235
236
|
},
value (val) {
this.currentIndex = val;
|
3e6d6356
Rijn
implement basic t...
|
237
238
|
}
},
|
bb71140e
梁灏
support Carousel
|
239
240
|
mounted () {
this.updateSlides(true);
|
65c64ce9
Rijn
carousel basic po...
|
241
|
this.handleResize();
|
bfc11079
Rijn
updated autoplay ...
|
242
|
this.setAutoplay();
|
65c64ce9
Rijn
carousel basic po...
|
243
244
245
246
|
window.addEventListener('resize', this.handleResize, false);
},
beforeDestroy () {
window.removeEventListener('resize', this.handleResize, false);
|
41f83010
Rijn
update props and ...
|
247
|
}
|
6c9acb08
Rijn
initialize carousel
|
248
249
|
};
</script>
|