forked from wesbos/JavaScript30
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
81 lines (67 loc) · 1.94 KB
/
index.js
File metadata and controls
81 lines (67 loc) · 1.94 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
const cardCarousel = document.querySelector('.card_container');
const buttons = [].slice.call(document.querySelectorAll('.page'));
const cards = [].slice.call(document.querySelectorAll('.card'));
let count = 0;
let previous;
let paused = false;
let carouselInterval;
const onLoad = () => {
buttons[0].classList.add('active');
cards[0].classList.add('active');
setTimeout(() => {
console.log('👀 👀 👀 👀 👀');
playAnimation();
}, 100);
}
function stopAnimation(cardCarousel) {
console.log('⏱⏱⏱⏱⏱⏱⏱⏱');
cardCarousel.classList.add('paused');
return clearInterval(carouselInterval);
}
function playAnimation() {
console.log('🏃🏃🏃🏃🏃🏃🏃🏃🏃');
clearInterval(carouselInterval);
carouselInterval = setInterval(() => {
autoLoop();
}, 2500);
}
const activeCards = (event) => {
cards.forEach((card) => {
if (card.getAttribute('data-id') === event.getAttribute('data-id')) {
card.classList.add('active');
} else {
card.classList.remove('active');
}
});
};
const activeButtons = (event) => {
paused = true;
event.preventDefault();
if (paused) {
stopAnimation(cardCarousel);
}
buttons.forEach((button) => {
button.classList.remove('active');
button.setAttribute('aria-selected', 'false');
});
const activeButton = event.target;
activeCards(activeButton);
activeButton.classList.add('active');
activeButton.setAttribute('aria-selected', 'true');
};
function autoLoop() {
previous = count > 0 ? count - 1 : 0;
buttons[previous].classList.toggle('active');
cards[previous].classList.toggle('active');
if (count < buttons.length) {
buttons[count].classList.add('active');
cards[count].classList.add('active');
count++;
} else {
count = 0;
autoLoop();
}
}
function startListening() { window.addEventListener('load', onLoad); }
buttons.forEach(button => button.addEventListener('click', activeButtons));
startListening();