-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathCarousel.vue
228 lines (211 loc) · 6.29 KB
/
Carousel.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<template>
<div class="card-carousel" @mouseover="stopTimer" @mouseleave="restartTimer">
<div class="progressbar" v-if="autoSlideInterval && showProgressBar">
<div :style="{width: progressBar + '%' }"></div>
</div>
<div class="card-img">
<img :src="currentImage" alt="">
<div class="actions">
<span @click="prevImage" class="prev">
‹
</span>
<span @click="nextImage" class="next">
›
</span>
</div>
</div>
<div class="thumbnails">
<div
v-for="(image, index) in images"
:key="image.id"
:class="['thumbnail-image', (activeImage == index) ? 'active' : '']"
@click="activateImage(index)"
>
<img :src="image.thumb">
</div>
</div>
</div>
</template>
<script>
export default {
name: 'Carousel',
data() {
return {
//Index of the active image
activeImage: 0,
//Hold the timeout, so we can clear it when it is needed
autoSlideTimeout: null,
//If the timer is stopped e.g. when hovering over the carousel
stopSlider: false,
//Hold the time left until changing to the next image
timeLeft: 0,
//Hold the interval so we can clear it when needed
timerInterval: null,
//Every 10ms decrease the timeLeft
countdownInterval: 10
}
},
computed: {
// currentImage gets called whenever activeImage changes
// and is the reason why we don't have to worry about the
// big image getting updated
currentImage() {
this.timeLeft = this.autoSlideInterval;
return this.images[this.activeImage].big;
},
progressBar() {
//Calculate the width of the progressbar
return 100 - (this.timeLeft/this.autoSlideInterval) * 100;
}
},
methods: {
// Go forward on the images array
// or go at the first image if you can't go forward
nextImage() {
var active = this.activeImage + 1;
if(active >= this.images.length) {
active = 0;
}
this.activateImage(active);
},
// Go backwards on the images array
// or go at the last image
prevImage() {
var active = this.activeImage - 1;
if(active < 0) {
active = this.images.length - 1;
}
this.activateImage(active);
},
activateImage(imageIndex) {
this.activeImage = imageIndex;
},
//Wait until 'interval' and go to the next image;
startTimer(interval) {
if(interval && interval > 0 && !this.stopSlider) {
var self = this;
clearTimeout(this.autoSlideTimeout);
this.autoSlideTimeout = setTimeout(function() {
self.nextImage();
self.startTimer(self.autoSlideInterval);
}, interval);
}
},
//Stop the timer when hovering over the carousel
stopTimer() {
clearTimeout(this.autoSlideTimeout);
this.stopSlider = true;
clearInterval(this.timerInterval);
},
//Restart the timer(with 'timeLeft') when leaving from the carousel
restartTimer() {
this.stopSlider = false;
clearInterval(this.timerInterval);
this.startCountdown();
this.startTimer(this.timeLeft);
},
//Start countdown from 'autoSlideInterval' to 0
startCountdown() {
if(!this.showProgressBar) return;
var self = this;
this.timerInterval = setInterval(function() {
self.timeLeft -= self.countdownInterval;
if(self.timeLeft <= 0) {
self.timeLeft = self.autoSlideInterval;
}
}, this.countdownInterval);
}
},
created() {
//Check if startingImage prop was given and if the index is inside the images array bounds
if(this.startingImage
&& this.startingImage >= 0
&& this.startingImage < this.images.length) {
this.activeImage = this.startingImage;
}
//Check if autoSlideInterval prop was given and if it is a positive number
if(this.autoSlideInterval
&& this.autoSlideInterval > this.countdownInterval) {
//Start the timer to go to the next image
this.startTimer(this.autoSlideInterval);
this.timeLeft = this.autoSlideInterval;
//Start countdown to show the progressbar
this.startCountdown();
}
},
props: ['startingImage', 'images', 'autoSlideInterval', 'showProgressBar']
}
</script>
<style scoped>
.card-carousel {
user-select: none;
position: relative;
}
.progressbar {
display: block;
width: 100%;
height: 5px;
position: absolute;
background-color: rgba(221, 221, 221, 0.25);
z-index: 1;
}
.progressbar > div {
background-color: rgba(255, 255, 255, 0.52);
height: 100%;
}
.thumbnails {
display: flex;
justify-content: space-evenly;
flex-direction: row;
}
.thumbnail-image {
display: flex;
align-items: center;
cursor: pointer;
padding: 2px;
}
.thumbnail-image > img {
width: 100%;
height: auto;
transition: all 250ms;
}
.thumbnail-image:hover > img,
.thumbnail-image.active > img {
opacity: 0.6;
box-shadow: 2px 2px 6px 1px rgba(0,0,0, 0.5);
}
.card-img {
position: relative;
margin-bottom: 15px;
}
.card-img > img {
display: block;
margin: 0 auto;
}
.actions {
font-size: 1.5em;
height: 40px;
position: absolute;
top: 50%;
margin-top: -20px;
width: 100%;
display: flex;
align-items: center;
justify-content: space-between;
color: #585858;
}
.actions > span {
cursor: pointer;
transition: all 250ms;
font-size: 45px;
}
.actions > span.prev {
margin-left: 5px;
}
.actions > span.next {
margin-right: 5px;
}
.actions > span:hover {
color: #eee;
}
</style>