-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.js
72 lines (64 loc) · 2.28 KB
/
script.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
70
71
72
function getButton(id) {
// id + "-btn"
return document.getElementById(id + "-btn");
};
function getSnapElement(btnId) {
// btnId - "-btn"
return document.getElementById(btnId.slice(0, -4));
};
function getSiblingButtons(btn) {
let result = [];
let btns = btn.parentElement.children;
for (let i = 0; i < btns.length; i++) {
if (btns[i] != btn) {
result.push(btns[i]);
}
}
return result;
}
// get scroll position of horizontal scroll of snappy-scroll-container.
// then, depending on that, set one of the buttons as active and the rest as inactive
function setButtonsStateForSnappyScroll(container, btns) {
let scrollPos = getScrollPercentage(container);
let btnIdx = Math.floor(scrollPos * 5);
for (let i = 0; i < btns.length; i++) {
if (i == btnIdx) {
btns[i].classList.add("active");
} else {
btns[i].classList.remove("active");
}
}
}
function getScrollPercentage(container) {
let scrollPos = container.scrollLeft;
// get number between [0, 1)
let result = scrollPos / (container.scrollWidth - container.clientWidth);
if (result === 1) { result = 0.99 };
return result;
}
// run this above function whenever scroll changes
function setButtonsStateForSnappyScrollOnChange(container, btns) {
container.addEventListener("scroll", function(e) {
setButtonsStateForSnappyScroll(container, btns);
});
}
let snappyScrollContainer1 = document.getElementsByClassName("snappy-scroll-container")[0];
let snappyScrollButtons1 = document.getElementsByClassName("snappy-scroll-nav-button");
snappyScrollContainer1.addEventListener("scroll", function(e) {
setButtonsStateForSnappyScroll(e.currentTarget, snappyScrollButtons1);
});
// on click each button, scroll to the corresponding element
function snappyButtonOnClick(btn) {
let snapElement = getSnapElement(btn.id);
// only do horizontal scroll, no vertical scroll
snappyScrollContainer1.scrollTo({
left: snapElement.offsetLeft,
behavior: "smooth"
});
}
// do this for all buttons with class "snappy-scroll-nav-button"
Array.from(snappyScrollButtons1).forEach(function(btn) {
btn.addEventListener("click", function(e) {
snappyButtonOnClick(e.currentTarget);
});
});