carousel.vue
5.57 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<template>
<div class="carousel slide" data-ride="carousel" @mouseenter="pause()" @mouseleave="start()">
<!-- Indicators -->
<ol class="carousel-indicators" v-show="indicators">
<li v-for="(item,indicatorIndex) in slides" :class="{active:indicatorIndex === index}" @click="changeSlide(indicatorIndex)"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<slot></slot>
</div>
<!-- Controls -->
<a class="left carousel-control" href="#" role="button" @click.stop.prevent="prev" v-show="controls">
<span class="icon-prev" aria-hidden="true"> </span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#" role="button" @click.stop.prevent="next" v-show="controls">
<span class="icon-next" aria-hidden="true"> </span>
<span class="sr-only">Next</span>
</a>
</div>
</template>
<script>
/**
* Carousel Notes
* - Ie9 does not support transitions and might require javascript fallbacks. B4 deliberately dropped support for this.
* - It is not accessible.
*
* How it works:
* - active element applies the transition to the slide but not triggers it
* - we need to use 'right' and 'left' classes to trigger animation
* - 'next' and 'prev' class makes the incoming slide positioned absolute, so it can follow outgoing slide
*
* To slide right to left we have to:
* - add class 'active', 'next', and right to the next slide
* - add class 'left' on the current slide same time as remove the 'right' class on the incoming one
* - remove all classes and only leave 'active' on the incoming slide
*
*/
import {csstransitions} from '../utils/helpers';
// this is directly linked to the bootstrap animation timing in _carousel.scss
// for browsers that do not support transitions like IE9 just change slide immediately
const TRANSITION_DURATION = csstransitions() ? 600 : 0;
// when next is set, we want to move from right to left
// when previous is set, we want to move from left to right
const DIRECTION = {
rtl: {
outgoing: 'left',
incoming: 'right',
overlay: 'next'
},
ltr: {
outgoing: 'right',
incoming: 'left',
overlay: 'prev'
}
};
export default {
replace: true,
computed: {},
data() {
return {
index: 0,
slidesCount: 0,
animating: false,
slides: [],
direction: DIRECTION.rtl
};
},
props: {
interval: {
type: Number,
default: 5000
},
indicators: {
type: Boolean,
default: true
},
controls: {
type: Boolean,
default: true
}
},
methods: {
// previous slide
prev() {
if (this.animating) {
return;
}
this.index--;
if (this.index < 0) {
this.index = this.slidesCount;
}
},
// next slide
next() {
if (this.animating) {
return;
}
this.index++;
if (this.index > this.slidesCount) {
this.index = 0;
}
},
// on slide change
changeSlide(index) {
this.index = index;
},
// pause auto rotation
pause() {
if (this.interval === 0 || typeof this.interval === 'undefined') {
return;
}
clearInterval(this._intervalId);
},
// start auto rotate slides
start() {
if (this.interval === 0 || typeof this.interval === 'undefined') {
return;
}
this._intervalId = setInterval(() => {
this.next();
}, this.interval);
}
},
mounted() {
// get all slides
this._items = this.$el.querySelectorAll('.carousel-item');
this.slidesCount = this._items.length - 1;
this.slides = Array.apply(null, {length: this._items.length}).map(Number.call, Number);
// set first slide as active
this._items[0].classList.add('active');
// auto rotate slides
this.start();
},
watch: {
index(val, oldVal) {
this.animating = true;
this.direction = DIRECTION.rtl;
// when previous is pressed we want to move from left to right
if (val < oldVal) {
this.direction = DIRECTION.ltr;
}
// lets animate
// prepare next slide to animate (position it on the opposite side of the direction as a starting point)
this._items[val].classList.add(this.direction.incoming, this.direction.overlay);
// reflow
// this._items[val].offsetWidth;
// add class active
this._items[val].classList.add('active');
// trigger animation on outgoing and incoming slide
this._items[oldVal].classList.add(this.direction.outgoing);
this._items[val].classList.remove(this.direction.incoming);
// wait for animation to finish and cleanup classes
this._carouselAnimation = setTimeout(() => {
this._items[oldVal].classList.remove(this.direction.outgoing, 'active');
this._items[val].classList.remove(this.direction.overlay);
this.animating = false;
// trigger an event
this.$root.$emit('slid::carousel', val);
}, TRANSITION_DURATION);
}
},
destroyed() {
clearTimeout(this._carouselAnimation);
clearInterval(this._intervalId);
}
};
</script>