-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (54 loc) · 1.75 KB
/
index.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
class GalleryCardsSlider {
constructor(config) {
this.activeCardIndex = config.activeCardIndex ?? 0;
this.transition = config.transition ?? "flex 500ms ease-in-out";
this.#init();
}
#init() {
this.#getAllCards();
this.#addListener();
}
#getAllCards() {
this.cards = document.querySelectorAll('.card');
this.cards.forEach((card, index) => {
if(this.activeCardIndex === index) card.classList.add('card_active');
card.dataset.index = index;
// Add transition to each card.
// Wrap it in setTimeout to remove transition
// during setting the first card_active.
setTimeout(() => {
this.#addTransition(card)
}, 0)
// set tabindex for accessibility from keyboard
card.setAttribute("tabindex", "0");
})
}
#initChanging(e) {
if(!e.target.classList.contains('card_active') && e.target.classList.contains('card')) {
const index = Number(e.target.dataset.index);
this.#changeActiveCard(index)
}
}
#addListener() {
const container = document.querySelector('#container');
container.addEventListener('click', this.#initChanging.bind(this));
// use focusin because we need bubbling
container.addEventListener('focusin', this.#initChanging.bind(this));
}
#changeActiveCard(newIndex) {
// Remove active status from previous card.
this.cards[this.activeCardIndex].classList.remove('card_active');
// Set active status to new card.
this.cards[newIndex].classList.add('card_active');
// Save new index.
this.activeCardIndex = newIndex;
}
#addTransition(card) {
card.style.transition = this.transition;
}
}
const config = {
activeCardIndex: 0,
transition: "flex 500ms ease-in-out"
}
new GalleryCardsSlider(config)