-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarousel.js
69 lines (60 loc) · 1.89 KB
/
carousel.js
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
const carousel = document.querySelector('.carousel');
const mediaContainers = document.querySelectorAll('.media-container');
const prevBtns = document.querySelectorAll('.prev-btn');
const nextBtns = document.querySelectorAll('.next-btn');
const media = document.querySelectorAll('.media');
const playButtons = document.querySelectorAll('.play-button');
const pauseButtons = document.querySelectorAll('.pause-button');
let currentMediaIndex = 0;
function pauseMedia(index) {
media[index].pause();
}
playButtons.forEach((button) => {
button.addEventListener('click', (event) => {
const index = event.target.dataset.mediaIndex;
playMedia(index);
});
});
pauseButtons.forEach((button) => {
button.addEventListener('click', (event) => {
const index = event.target.dataset.mediaIndex;
pauseMedia(index);
});
});
function showMedia() {
mediaContainers.forEach((mediaContainer, index) => {
if (index === currentMediaIndex) {
mediaContainer.classList.add('active');
if (media[index].tagName === 'VIDEO') {
media[index].play();
}
} else {
mediaContainer.classList.remove('active');
if (media[index].tagName === 'VIDEO') {
media[index].pause();
media[index].currentTime = 0;
}
}
});
}
prevBtns.forEach(prevBtn => {
prevBtn.addEventListener('click', () => {
if (currentMediaIndex === 0) {
currentMediaIndex = mediaContainers.length - 1;
} else {
currentMediaIndex--;
}
showMedia();
});
});
nextBtns.forEach(nextBtn => {
nextBtn.addEventListener('click', () => {
if (currentMediaIndex === mediaContainers.length - 1) {
currentMediaIndex = 0;
} else {
currentMediaIndex++;
}
showMedia();
});
});
showMedia();