-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwt-snapscroll-simple.js
More file actions
59 lines (44 loc) · 2.09 KB
/
wt-snapscroll-simple.js
File metadata and controls
59 lines (44 loc) · 2.09 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
window.addEventListener('load', () => {
/* ===================================================
SECTION 1: The "Snap Zone" Logic
=================================================== */
const html = document.documentElement;
const snapItems = document.querySelectorAll('.wt-snapscroll-item');
if (snapItems.length === 0) return;
const firstSection = snapItems[0];
const lastSection = snapItems[snapItems.length - 1];
function checkSnapScroll() {
const viewportHeight = window.innerHeight;
// We use a buffer (+50/-50) so the snap doesn't disengage
// too early while the user is still finishing a swipe.
const firstTop = firstSection.getBoundingClientRect().top + 50;
const lastBottom = lastSection.getBoundingClientRect().bottom - 50;
// Logic:
// 1. Have we scrolled down past the top of the first item? (firstTop <= 0)
// 2. Have we NOT yet scrolled past the bottom of the last item? (lastBottom >= viewportHeight)
const isInSnapZone = (firstTop <= 0) && (lastBottom >= viewportHeight);
// Toggle the class.
// The second argument (boolean) ensures we don't add/remove unnecessarily.
html.classList.toggle('wt-snapscroll-zone', isInSnapZone);
}
// OPTIMIZATION: Use { passive: true } for better mobile scrolling performance
window.addEventListener('scroll', checkSnapScroll, { passive: true });
// Check once on load
checkSnapScroll();
/* ===================================================
SECTION 2: Active Class Observer
=================================================== */
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
// Optional: You could use a more specific selector here if needed
// but your current loop is fine for small numbers of items.
snapItems.forEach(item => item.classList.remove('wt-snapscroll-active'));
entry.target.classList.add('wt-snapscroll-active');
}
});
}, {
threshold: 0.5
});
snapItems.forEach(item => observer.observe(item));
});