generated from tuskydev/EXAMPLE-README-SOLO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
331 lines (270 loc) · 9.62 KB
/
index.js
File metadata and controls
331 lines (270 loc) · 9.62 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/* ----------
- scrollFunction runs everytime the user scrolls
- Scrolling down 30px resizes the navbar's padding
---------- */
window.onscroll = function () {
scrollFunction();
};
function scrollFunction() {
if (document.body.scrollTop > 30 || document.documentElement.scrollTop > 30) {
document.getElementsByClassName("navBarUL")[0].style.paddingTop = ".777rem";
} else {
// Access the first element with class "navBarUL" and set its padding
document.getElementsByClassName("navBarUL")[0].style.paddingTop = "1rem";
}
}
/* ----------
- Initially, all content is invisible
---------- */
document.fonts.ready.then(() => {
document.body.style.opacity = "1";
});
function triggerRepaint() {
document.body.style.overflow = "hidden";
setTimeout(() => {
document.body.style.overflow = "";
}, 10);
}
/* ----------
- typingAnimationFunction reconstructs the element with <spans> of the element's length
- Checking header type to assign font-family and CSS
- After a tiny delay changes the opacity from 0 to 1 of each letter
- Creates a 'typing' effect
- Cleans up span tags with the original content for easier load on memory
- Function will NOT run until all fonts are loaded in
---------- */
document.fonts.ready.then(() => {
setTimeout(() => {
// One second delay
const typingObserver = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const element = entry.target;
const text = element.textContent; // Get the text content of the element
element.textContent = ""; // Clear the element content
// Create a span for each character in the text
for (let i = 0; i < text.length; i++) {
let span = document.createElement("span");
span.textContent = text[i];
span.style.opacity = "0"; // Start with the character hidden
element.appendChild(span);
}
// Make the element visible just before starting the animation
element.style.opacity = "1";
// Reveal each character with a delay
const spans = element.querySelectorAll("span");
spans.forEach((span, index) => {
setTimeout(() => {
if (
(element.tagName.toLowerCase() == "h1") |
(element.tagName.toLowerCase() == "h2")
) {
span.style.fontFamily = '"Alliance No. 2"';
}
if (element.tagName.toLowerCase() == "h3") {
span.style.fontFamily = '"Alliance No. 1"';
}
span.style.opacity = "1"; // Make the character visible
span.style.color = "#1e2124"; // Change to the desired color
}, index * 25); // Adjust the delay as needed
});
setTimeout(() => {
element.textContent = text;
element.style.color = "#1e2124";
}, text.length * 25 + 100); // Adjusted to wait until the last character is shown
typingObserver.unobserve(entry.target);
}
});
},
{ threshold: 1 }
);
const typingAnimationFunction =
document.querySelectorAll(".animationTyping");
typingAnimationFunction.forEach((el) => typingObserver.observe(el));
}, 111);
});
/* ----------
- animationBorderFunction selects all the .animationBorder classes in the DOM.
- Showing the element adds the class 'animationBorderDONE'
- Happens once per element
- Allowing for the animation to essentially be 'attached'
---------- */
const borderObserver = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const element = entry.target;
// Animate the border
element.classList.add("animationBorderDONE");
// Stop observing the element after applying the animation
borderObserver.unobserve(entry.target);
}
});
},
// How much in page before applying class
{ threshold: 1 }
);
const animationBorderFunction = document.querySelectorAll(".animationBorder");
animationBorderFunction.forEach((el) => borderObserver.observe(el));
/* ----------
- animationFadeUpOneFunction selects all the .animationFadeUpOne classes in the DOM.
- Showing the element adds the class 'animationFadeUpOneDONE'
- Happens once per element
- Allowing for the animation to essentially be 'attached'
---------- */
const fadeUpOneObserver = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const element = entry.target;
// Animate the border after a delay
setTimeout(() => {
element.classList.add("animationFadeUpOneDONE");
}, 222);
// Stop observing the element after applying the animation
fadeUpOneObserver.unobserve(entry.target);
}
});
},
// How much in page before applying class
{ threshold: 1 }
);
const animationFadeUpOneFunction = document.querySelectorAll(
".animationFadeUpOne"
);
animationFadeUpOneFunction.forEach((el) => fadeUpOneObserver.observe(el));
/* ----------
- animationFadeUpTwoFunction selects all the .animationFadeUpTwo classes in the DOM.
- Showing the element adds the class 'animationFadeUpTwoDONE'
- Happens once per element
- Allowing for the animation to essentially be 'attached'
---------- */
const fadeUpTwoObserver = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const element = entry.target;
// Animate the border after a delay
setTimeout(() => {
element.classList.add("animationFadeUpTwoDONE");
}, 777);
// Stop observing the element after applying the animation
fadeUpTwoObserver.unobserve(entry.target);
}
});
},
// How much in page before applying class
{ threshold: 1 }
);
const animationFadeUpTwoFunction = document.querySelectorAll(
".animationFadeUpTwo"
);
animationFadeUpTwoFunction.forEach((el) => fadeUpTwoObserver.observe(el));
/* ----------
- addAnimation selects all the .animationSkillArticle classes in the DOM.
- Adding the 'data-animated' attribute to them
- Cloning each <article> and appending it to the existing list once only
- Creating an infinite scroll!
---------- */
const animationSkillArticleFunction = document.querySelectorAll(
".animationSkillArticle"
);
// Adds animation if the user doesn't prefer reduced motion
if (!window.matchMedia("(prefers-reduced-motion: reduce").matches) {
addAnimation();
}
function addAnimation() {
animationSkillArticleFunction.forEach((el) => {
el.setAttribute("data-animated", true);
const scrollerInner = el.querySelector(".animationSkillArticleINNER");
const scrollerContent = Array.from(scrollerInner.children);
scrollerContent.forEach((item) => {
const duplicatedItem = item.cloneNode(true);
duplicatedItem.setAttribute("aria-hidden", true);
scrollerInner.append(duplicatedItem);
});
});
}
/* ----------
- navBarAnchorClickFunction adds a 'click' eventListener to all navBar <a>
- Sending user to the desired page area
- Clicking on 'Contact' adds the class .animationGlow
- Adds smooth-scrolling!
---------- */
function navBarAnchorClickFunction(e) {
e.preventDefault();
const href = this.getAttribute("href");
const navbarHeight = document.querySelector("nav").offsetHeight;
// Determine if the link is meant to scroll to the top
if (href === "#") {
window.scroll({
top: 0, // Scroll to the top of the page
behavior: "smooth",
});
} else {
const target = document.querySelector(href);
window.scroll({
top: target.offsetTop - navbarHeight,
behavior: "smooth",
});
// Apply the glow effect if it's the Contact link
if (target.id === "contactID") {
// Find the closest ancestor which is a section with class 'contactSector'
const contactSection = target.closest(".contactSector");
contactSection.classList.add("animationGlow");
// Remove the class after the animation completes (2 seconds)
setTimeout(() => {
contactSection.classList.remove("animationGlow");
}, 2000);
}
}
}
document.addEventListener("DOMContentLoaded", function () {
document.querySelectorAll('a[href^="#"]').forEach((anchor) => {
anchor.addEventListener("click", navBarAnchorClickFunction);
});
});
/* ----------
- projectLinkClickFunction adds a 'click' eventListener to all projectLinks
- Grabs the url from html attrbute 'data-url'
- Sending user to the desired website page
- Adds smooth-scrolling!
---------- */
function projectLinkClickFunction(e) {
e.preventDefault(); // Prevent the default link behavior
const url = this.getAttribute("data-url");
if (url) {
// Check if the URL is present
window.open(url, "_blank");
}
}
document.addEventListener("DOMContentLoaded", function () {
document.querySelectorAll(".projectLink").forEach((link) => {
link.addEventListener("click", projectLinkClickFunction);
});
});
/* ----------
- PWA RELATED:
---------- */
if ("serviceWorker" in navigator) {
window.addEventListener("load", function () {
navigator.serviceWorker.register("/sw.js").then(
(registration) => {
console.log(
"ServiceWorker registration successful with scope: ",
registration.scope
);
},
(err) => {
console.log("ServiceWorker registration failed: ", err);
}
);
});
}
let deferredPrompt;
window.addEventListener("beforeinstallprompt", (e) => {
e.preventDefault();
deferredPrompt = e;
// Update UI to notify the user they can add to the home screen
});