-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeatures-carousel.js
More file actions
176 lines (155 loc) · 5.18 KB
/
Copy pathfeatures-carousel.js
File metadata and controls
176 lines (155 loc) · 5.18 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
/**
* Features carousel — horizontal scroll-snap for #features only.
* Arrows, dots, keyboard; native swipe via scroll-snap.
*/
(function () {
'use strict';
function prefersReducedMotion() {
return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
}
function initCarousel(root) {
const track = root.querySelector('.features-carousel-track');
const prevBtn = root.querySelector('.features-carousel-btn.prev');
const nextBtn = root.querySelector('.features-carousel-btn.next');
const dotsHost = root.querySelector('.features-carousel-dots');
if (!track || !prevBtn || !nextBtn || !dotsHost) return;
const cards = () => Array.from(track.querySelectorAll('.feature-card'));
function cardStep() {
const list = cards();
if (!list.length) return track.clientWidth;
const first = list[0];
const gap = parseFloat(getComputedStyle(track).gap) || 0;
return first.getBoundingClientRect().width + gap;
}
function maxScroll() {
return Math.max(0, track.scrollWidth - track.clientWidth);
}
function scrollToIndex(index, instant) {
const list = cards();
if (!list.length) return;
const i = Math.max(0, Math.min(index, list.length - 1));
const left = list[i].offsetLeft - track.offsetLeft;
track.scrollTo({
left,
behavior: instant || prefersReducedMotion() ? 'auto' : 'smooth'
});
}
function nearestIndex() {
const list = cards();
if (!list.length) return 0;
const left = track.scrollLeft;
let best = 0;
let bestDist = Infinity;
list.forEach((card, i) => {
const dist = Math.abs(card.offsetLeft - track.offsetLeft - left);
if (dist < bestDist) {
bestDist = dist;
best = i;
}
});
return best;
}
function pageCount() {
const list = cards();
if (!list.length) return 0;
const step = cardStep();
if (step <= 0) return list.length;
const visible = Math.max(1, Math.round(track.clientWidth / step));
return Math.max(1, list.length - visible + 1);
}
function rebuildDots() {
const pages = pageCount();
dotsHost.innerHTML = '';
for (let i = 0; i < pages; i++) {
const btn = document.createElement('button');
btn.type = 'button';
btn.className = 'features-carousel-dot';
btn.setAttribute('role', 'tab');
btn.setAttribute('aria-label', 'Go to slide group ' + (i + 1));
btn.setAttribute('aria-selected', 'false');
btn.addEventListener('click', () => scrollToIndex(i));
dotsHost.appendChild(btn);
}
syncUi();
}
function syncUi() {
const index = nearestIndex();
const max = maxScroll();
const atStart = track.scrollLeft <= 2;
const atEnd = track.scrollLeft >= max - 2;
prevBtn.disabled = atStart;
nextBtn.disabled = atEnd;
const dots = dotsHost.querySelectorAll('.features-carousel-dot');
const pages = dots.length;
const pageIndex = Math.min(index, Math.max(0, pages - 1));
dots.forEach((dot, i) => {
const active = i === pageIndex;
dot.classList.toggle('is-active', active);
dot.setAttribute('aria-selected', active ? 'true' : 'false');
});
}
prevBtn.addEventListener('click', () => {
track.scrollBy({
left: -cardStep(),
behavior: prefersReducedMotion() ? 'auto' : 'smooth'
});
});
nextBtn.addEventListener('click', () => {
track.scrollBy({
left: cardStep(),
behavior: prefersReducedMotion() ? 'auto' : 'smooth'
});
});
let scrollRaf = 0;
track.addEventListener(
'scroll',
() => {
if (scrollRaf) cancelAnimationFrame(scrollRaf);
scrollRaf = requestAnimationFrame(syncUi);
},
{ passive: true }
);
track.addEventListener('keydown', (e) => {
if (e.key === 'ArrowLeft') {
e.preventDefault();
prevBtn.click();
} else if (e.key === 'ArrowRight') {
e.preventDefault();
nextBtn.click();
} else if (e.key === 'Home') {
e.preventDefault();
scrollToIndex(0);
} else if (e.key === 'End') {
e.preventDefault();
const list = cards();
scrollToIndex(list.length - 1);
}
});
if (!track.hasAttribute('tabindex')) {
track.setAttribute('tabindex', '0');
}
let resizeTimer = 0;
const onResize = () => {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(() => {
rebuildDots();
syncUi();
}, 100);
};
window.addEventListener('resize', onResize);
if (typeof ResizeObserver !== 'undefined') {
const ro = new ResizeObserver(onResize);
ro.observe(track);
}
rebuildDots();
syncUi();
}
function boot() {
document.querySelectorAll('[data-features-carousel]').forEach(initCarousel);
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', boot);
} else {
boot();
}
})();