-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
171 lines (143 loc) · 4.75 KB
/
Copy pathscript.js
File metadata and controls
171 lines (143 loc) · 4.75 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
const downloadButton = document.querySelector("#download-cv");
const downloadFAB = document.querySelector("#download-cv-fab");
const changeThemeButton = document.querySelector("#change-theme");
const changeThemeIcon = document.querySelector("#change-theme-icon");
const sections = document.querySelectorAll("section, #summary");
const navRailLinks = document.querySelectorAll(".nav-rail-link");
const navBarLinks = document.querySelectorAll(".nav-bar-link");
const skillSegmentedButtons = document.querySelectorAll(
".skills-segmented > nav > button",
);
const skillSections = document.querySelectorAll(".skills-content");
const scrollSpeedThreshold = 12;
const minScrollDistance = 0;
let lastScrollTop = 0;
let lastScrollTime = Date.now();
function downloadCV(e) {
e.preventDefault();
const fileUrl = "assets/GLEB_ANDREEV_CV.pdf";
const fileName = "GLEB_ANDREEV_CV.pdf";
fetch(fileUrl)
.then((response) => {
return response.blob();
})
.then((blob) => {
const url = window.URL.createObjectURL(
new Blob([blob], { type: "application/pdf" }),
);
const link = document.createElement("a");
link.href = url;
link.download = fileName;
link.style.display = "none";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
window.URL.revokeObjectURL(url);
})
.catch(() => {
alert("Failed to download file");
});
}
function setActiveSection() {
let currentSection = "";
const scrollPosition = window.scrollY;
const windowHeight = window.innerHeight;
const links = [...navRailLinks, ...navBarLinks];
const lastSectionMinVisible = 200;
sections.forEach((section, index) => {
if (index === sections.length - 1) {
const sectionRect = section.getBoundingClientRect();
const visibleHeight =
Math.min(sectionRect.bottom, windowHeight) -
Math.max(sectionRect.top, 0);
if (visibleHeight >= lastSectionMinVisible) {
currentSection = section.id;
}
} else {
const sectionTop = section.offsetTop;
const sectionHeight = section.clientHeight;
if (
scrollPosition >= sectionTop - 100 &&
scrollPosition < sectionTop + sectionHeight - 100
) {
currentSection = section.id;
}
}
});
links.forEach((link) => {
link.classList.remove("active");
});
if (currentSection) {
const activeRailLink = document.querySelector(
`.nav-rail a[href="#${currentSection}"]`,
);
const activeBarLink = document.querySelector(
`.nav-bar a[href="#${currentSection}"]`,
);
if (activeRailLink) {
activeRailLink.classList.add("active");
}
if (activeBarLink) {
activeBarLink.classList.add("active");
}
}
}
function setChangeThemeIcon() {
const currentTheme = ui("mode");
const icon = currentTheme === "dark" ? "light_mode" : "dark_mode";
changeThemeIcon.textContent = icon;
}
function changeTheme() {
const currentTheme = ui("mode");
const mode = currentTheme === "dark" ? "light" : "dark";
ui("mode", mode);
setChangeThemeIcon();
}
function handleChangeFAB() {
const currentTime = Date.now();
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
const maxScroll = document.documentElement.scrollHeight - window.innerHeight;
const isBouncing = scrollTop < 0 || scrollTop > maxScroll;
if (isBouncing) {
return;
}
if (Math.abs(scrollTop - lastScrollTop) < minScrollDistance) {
lastScrollTop = scrollTop;
return;
}
const timeDiff = currentTime - lastScrollTime;
const scrollDiff = Math.abs(scrollTop - lastScrollTop);
const scrollSpeed = timeDiff > 0 ? (scrollDiff / timeDiff) * 1000 : 0;
if (scrollSpeed <= scrollSpeedThreshold) {
return;
}
const scrollingDown = scrollTop > lastScrollTop;
if (scrollingDown) {
downloadFAB.classList.remove("active");
} else if (!scrollingDown) {
downloadFAB.classList.add("active");
}
lastScrollTop = scrollTop;
lastScrollTime = currentTime;
}
window.addEventListener("scroll", setActiveSection);
window.addEventListener("scroll", handleChangeFAB, { passive: true });
downloadButton.addEventListener("click", downloadCV);
downloadFAB.addEventListener("click", downloadCV);
changeThemeButton.addEventListener("click", changeTheme);
skillSegmentedButtons.forEach((button) => {
button.addEventListener("click", () => {
const targetId = button.getAttribute("data-target");
skillSegmentedButtons.forEach((btn) => btn.classList.remove("active"));
button.classList.add("active");
skillSections.forEach((section) => {
if (section.id === targetId) {
section.classList.add("active");
} else {
section.classList.remove("active");
}
});
});
});
setActiveSection();
setChangeThemeIcon();