-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
122 lines (105 loc) · 3.2 KB
/
script.js
File metadata and controls
122 lines (105 loc) · 3.2 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
const backToTopButton = document.getElementById("back-to-top");
const colorToggle = document.getElementById("color-toggle-input");
// Typing effect with typed.js
const typed = new Typed(".animate", {
strings: ["student.", "coder.", "game lover."],
typeSpeed: 100,
backSpeed: 100,
loop: true,
});
// Make navbar fixed
function stickyNav() {
const nav = document.querySelector("nav");
nav.classList.toggle("fixed", window.scrollY > 0);
}
// Lazy load background images
document.addEventListener("DOMContentLoaded", function () {
const lazyBackgrounds = [].slice.call(
document.querySelectorAll(".lazy-background")
);
if ("IntersectionObserver" in window) {
let lazyBackgroundObserver = new IntersectionObserver(function (
entries,
_
) {
entries.forEach(function (entry) {
if (entry.isIntersecting) {
entry.target.classList.add("visible");
lazyBackgroundObserver.unobserve(entry.target);
}
});
});
lazyBackgrounds.forEach(function (lazyBackground) {
lazyBackgroundObserver.observe(lazyBackground);
});
}
});
// Reveal projects
function revealProjects() {
const reveal = document.querySelector(".reveal");
const windowHeight = window.innerHeight;
const revealTop = reveal.getBoundingClientRect().top;
const revealPoint = 150;
if (revealTop < windowHeight - revealPoint) {
reveal.classList.add("active");
}
}
// Mobile hamburger-menu
function navSlider() {
const burgerMenu = document.querySelector(".hamburger-menu");
const nav = document.querySelector(".links");
const links = document.querySelectorAll(".links li");
burgerMenu.addEventListener("click", () => {
nav.classList.toggle("nav-active");
links.forEach((link, index) => {
if (link.style.animation) link.style.animation = "";
else {
link.style.animation = `linkFade 0.4s ease forwards ${
index / 7 + 0.5
}s`;
}
});
burgerMenu.classList.toggle("toggle");
});
}
navSlider();
// Button entrance and exit
function scrollDownFunction() {
if (window.pageYOffset > 1800) {
// Show backToTopButton
if (!backToTopButton.classList.contains("btn-entrance")) {
backToTopButton.classList.remove("btn-exit");
backToTopButton.classList.add("btn-entrance");
backToTopButton.style.display = "block";
}
} else {
// Hide backToTopButton
if (backToTopButton.classList.contains("btn-entrance")) {
backToTopButton.classList.remove("btn-entrance");
backToTopButton.classList.add("btn-exit");
setTimeout(() => {
backToTopButton.style.display = "none";
}, 250);
}
}
}
// Back to top button
function backToTop() {
window.scrollTo(0, 0);
}
// Toggle dark mode
function checkMode() {
if (colorToggle.checked) darkModeOn();
else darkModeOff();
}
function darkModeOn() {
document.body.classList.add("light-mode");
}
function darkModeOff() {
document.body.classList.remove("light-mode");
}
window.addEventListener("scroll", stickyNav);
window.addEventListener("scroll", revealProjects);
window.addEventListener("scroll", scrollDownFunction);
backToTopButton.addEventListener("click", backToTop);
colorToggle.addEventListener("click", checkMode);