1 | <template>
|
2 | <div
|
3 | :class="carouselClasses"
|
4 | @mouseenter.stop="handleMouseEnter"
|
5 | @mouseleave.stop="handleMouseLeave">
|
6 | <div
|
7 | class="el-carousel__container"
|
8 | :style="{ height: height }">
|
9 | <transition
|
10 | v-if="arrowDisplay"
|
11 | name="carousel-arrow-left">
|
12 | <button
|
13 | type="button"
|
14 | v-show="(arrow === 'always' || hover) && (loop || activeIndex > 0)"
|
15 | @mouseenter="handleButtonEnter('left')"
|
16 | @mouseleave="handleButtonLeave"
|
17 | @click.stop="throttledArrowClick(activeIndex - 1)"
|
18 | class="el-carousel__arrow el-carousel__arrow--left">
|
19 | <i class="el-icon-arrow-left"></i>
|
20 | </button>
|
21 | </transition>
|
22 | <transition
|
23 | v-if="arrowDisplay"
|
24 | name="carousel-arrow-right">
|
25 | <button
|
26 | type="button"
|
27 | v-show="(arrow === 'always' || hover) && (loop || activeIndex < items.length - 1)"
|
28 | @mouseenter="handleButtonEnter('right')"
|
29 | @mouseleave="handleButtonLeave"
|
30 | @click.stop="throttledArrowClick(activeIndex + 1)"
|
31 | class="el-carousel__arrow el-carousel__arrow--right">
|
32 | <i class="el-icon-arrow-right"></i>
|
33 | </button>
|
34 | </transition>
|
35 | <slot></slot>
|
36 | </div>
|
37 | <ul
|
38 | v-if="indicatorPosition !== 'none'"
|
39 | :class="indicatorsClasses">
|
40 | <li
|
41 | v-for="(item, index) in items"
|
42 | :key="index"
|
43 | :class="[
|
44 | 'el-carousel__indicator',
|
45 | 'el-carousel__indicator--' + direction,
|
46 | { 'is-active': index === activeIndex }]"
|
47 | @mouseenter="throttledIndicatorHover(index)"
|
48 | @click.stop="handleIndicatorClick(index)">
|
49 | <button class="el-carousel__button">
|
50 | <span v-if="hasLabel">{{ item.label }}</span>
|
51 | </button>
|
52 | </li>
|
53 | </ul>
|
54 | </div>
|
55 | </template>
|
56 |
|
57 | <script>
|
58 | import throttle from 'throttle-debounce/throttle';
|
59 | import { addResizeListener, removeResizeListener } from 'element-ui/src/utils/resize-event';
|
60 |
|
61 | export default {
|
62 | name: 'ElCarousel',
|
63 |
|
64 | props: {
|
65 | initialIndex: {
|
66 | type: Number,
|
67 | default: 0
|
68 | },
|
69 | height: String,
|
70 | trigger: {
|
71 | type: String,
|
72 | default: 'hover'
|
73 | },
|
74 | autoplay: {
|
75 | type: Boolean,
|
76 | default: true
|
77 | },
|
78 | interval: {
|
79 | type: Number,
|
80 | default: 3000
|
81 | },
|
82 | indicatorPosition: String,
|
83 | indicator: {
|
84 | type: Boolean,
|
85 | default: true
|
86 | },
|
87 | arrow: {
|
88 | type: String,
|
89 | default: 'hover'
|
90 | },
|
91 | type: String,
|
92 | loop: {
|
93 | type: Boolean,
|
94 | default: true
|
95 | },
|
96 | direction: {
|
97 | type: String,
|
98 | default: 'horizontal',
|
99 | validator(val) {
|
100 | return ['horizontal', 'vertical'].indexOf(val) !== -1;
|
101 | }
|
102 | }
|
103 | },
|
104 |
|
105 | data() {
|
106 | return {
|
107 | items: [],
|
108 | activeIndex: -1,
|
109 | containerWidth: 0,
|
110 | timer: null,
|
111 | hover: false
|
112 | };
|
113 | },
|
114 |
|
115 | computed: {
|
116 | arrowDisplay() {
|
117 | return this.arrow !== 'never' && this.direction !== 'vertical';
|
118 | },
|
119 |
|
120 | hasLabel() {
|
121 | return this.items.some(item => item.label.toString().length > 0);
|
122 | },
|
123 |
|
124 | carouselClasses() {
|
125 | const classes = ['el-carousel', 'el-carousel--' + this.direction];
|
126 | if (this.type === 'card') {
|
127 | classes.push('el-carousel--card');
|
128 | }
|
129 | return classes;
|
130 | },
|
131 |
|
132 | indicatorsClasses() {
|
133 | const classes = ['el-carousel__indicators', 'el-carousel__indicators--' + this.direction];
|
134 | if (this.hasLabel) {
|
135 | classes.push('el-carousel__indicators--labels');
|
136 | }
|
137 | if (this.indicatorPosition === 'outside' || this.type === 'card') {
|
138 | classes.push('el-carousel__indicators--outside');
|
139 | }
|
140 | return classes;
|
141 | }
|
142 | },
|
143 |
|
144 | watch: {
|
145 | items(val) {
|
146 | if (val.length > 0) this.setActiveItem(this.initialIndex);
|
147 | },
|
148 |
|
149 | activeIndex(val, oldVal) {
|
150 | this.resetItemPosition(oldVal);
|
151 | if (oldVal > -1) {
|
152 | this.$emit('change', val, oldVal);
|
153 | }
|
154 | },
|
155 |
|
156 | autoplay(val) {
|
157 | val ? this.startTimer() : this.pauseTimer();
|
158 | },
|
159 |
|
160 | loop() {
|
161 | this.setActiveItem(this.activeIndex);
|
162 | }
|
163 | },
|
164 |
|
165 | methods: {
|
166 | handleMouseEnter() {
|
167 | this.hover = true;
|
168 | this.pauseTimer();
|
169 | },
|
170 |
|
171 | handleMouseLeave() {
|
172 | this.hover = false;
|
173 | this.startTimer();
|
174 | },
|
175 |
|
176 | itemInStage(item, index) {
|
177 | const length = this.items.length;
|
178 | if (index === length - 1 && item.inStage && this.items[0].active ||
|
179 | (item.inStage && this.items[index + 1] && this.items[index + 1].active)) {
|
180 | return 'left';
|
181 | } else if (index === 0 && item.inStage && this.items[length - 1].active ||
|
182 | (item.inStage && this.items[index - 1] && this.items[index - 1].active)) {
|
183 | return 'right';
|
184 | }
|
185 | return false;
|
186 | },
|
187 |
|
188 | handleButtonEnter(arrow) {
|
189 | if (this.direction === 'vertical') return;
|
190 | this.items.forEach((item, index) => {
|
191 | if (arrow === this.itemInStage(item, index)) {
|
192 | item.hover = true;
|
193 | }
|
194 | });
|
195 | },
|
196 |
|
197 | handleButtonLeave() {
|
198 | if (this.direction === 'vertical') return;
|
199 | this.items.forEach(item => {
|
200 | item.hover = false;
|
201 | });
|
202 | },
|
203 |
|
204 | updateItems() {
|
205 | this.items = this.$children.filter(child => child.$options.name === 'ElCarouselItem');
|
206 | },
|
207 |
|
208 | resetItemPosition(oldIndex) {
|
209 | this.items.forEach((item, index) => {
|
210 | item.translateItem(index, this.activeIndex, oldIndex);
|
211 | });
|
212 | },
|
213 |
|
214 | playSlides() {
|
215 | if (this.activeIndex < this.items.length - 1) {
|
216 | this.activeIndex++;
|
217 | } else if (this.loop) {
|
218 | this.activeIndex = 0;
|
219 | }
|
220 | },
|
221 |
|
222 | pauseTimer() {
|
223 | if (this.timer) {
|
224 | clearInterval(this.timer);
|
225 | this.timer = null;
|
226 | }
|
227 | },
|
228 |
|
229 | startTimer() {
|
230 | if (this.interval <= 0 || !this.autoplay || this.timer) return;
|
231 | this.timer = setInterval(this.playSlides, this.interval);
|
232 | },
|
233 |
|
234 | setActiveItem(index) {
|
235 | if (typeof index === 'string') {
|
236 | const filteredItems = this.items.filter(item => item.name === index);
|
237 | if (filteredItems.length > 0) {
|
238 | index = this.items.indexOf(filteredItems[0]);
|
239 | }
|
240 | }
|
241 | index = Number(index);
|
242 | if (isNaN(index) || index !== Math.floor(index)) {
|
243 | console.warn('[Element Warn][Carousel]index must be an integer.');
|
244 | return;
|
245 | }
|
246 | let length = this.items.length;
|
247 | const oldIndex = this.activeIndex;
|
248 | if (index < 0) {
|
249 | this.activeIndex = this.loop ? length - 1 : 0;
|
250 | } else if (index >= length) {
|
251 | this.activeIndex = this.loop ? 0 : length - 1;
|
252 | } else {
|
253 | this.activeIndex = index;
|
254 | }
|
255 | if (oldIndex === this.activeIndex) {
|
256 | this.resetItemPosition(oldIndex);
|
257 | }
|
258 | },
|
259 |
|
260 | prev() {
|
261 | this.setActiveItem(this.activeIndex - 1);
|
262 | },
|
263 |
|
264 | next() {
|
265 | this.setActiveItem(this.activeIndex + 1);
|
266 | },
|
267 |
|
268 | handleIndicatorClick(index) {
|
269 | this.activeIndex = index;
|
270 | },
|
271 |
|
272 | handleIndicatorHover(index) {
|
273 | if (this.trigger === 'hover' && index !== this.activeIndex) {
|
274 | this.activeIndex = index;
|
275 | }
|
276 | }
|
277 | },
|
278 |
|
279 | created() {
|
280 | this.throttledArrowClick = throttle(300, true, index => {
|
281 | this.setActiveItem(index);
|
282 | });
|
283 | this.throttledIndicatorHover = throttle(300, index => {
|
284 | this.handleIndicatorHover(index);
|
285 | });
|
286 | },
|
287 |
|
288 | mounted() {
|
289 | this.updateItems();
|
290 | this.$nextTick(() => {
|
291 | addResizeListener(this.$el, this.resetItemPosition);
|
292 | if (this.initialIndex < this.items.length && this.initialIndex >= 0) {
|
293 | this.activeIndex = this.initialIndex;
|
294 | }
|
295 | this.startTimer();
|
296 | });
|
297 | },
|
298 |
|
299 | beforeDestroy() {
|
300 | if (this.$el) removeResizeListener(this.$el, this.resetItemPosition);
|
301 | this.pauseTimer();
|
302 | }
|
303 | };
|
304 | </script>
|