-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
171 lines (148 loc) · 5.06 KB
/
script.js
File metadata and controls
171 lines (148 loc) · 5.06 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
/* ===========================
Alex Khoo - Portfolio JS
Pure vanilla, no libraries
=========================== */
(function () {
"use strict";
// ---------- Scroll to top on refresh ----------
if (history.scrollRestoration) {
history.scrollRestoration = "manual";
}
window.scrollTo(0, 0);
// ---------- Theme toggle ----------
const themeToggle = document.getElementById("themeToggle");
const html = document.documentElement;
function applyTheme(theme) {
if (theme === "light") {
html.classList.remove("dark");
html.classList.add("light");
} else {
html.classList.remove("light");
html.classList.add("dark");
}
}
// Load saved theme
const savedTheme = localStorage.getItem("theme") || "light";
applyTheme(savedTheme);
themeToggle.addEventListener("click", function () {
const isDark = html.classList.contains("dark");
const next = isDark ? "light" : "dark";
applyTheme(next);
localStorage.setItem("theme", next);
});
// ---------- Mobile menu ----------
const mobileToggle = document.getElementById("mobileToggle");
const navLinks = document.getElementById("navLinks");
mobileToggle.addEventListener("click", function () {
const isOpen = navLinks.classList.toggle("open");
mobileToggle.classList.toggle("open", isOpen);
mobileToggle.setAttribute("aria-expanded", isOpen);
});
// Close mobile menu on link click
navLinks.querySelectorAll("a").forEach(function (link) {
link.addEventListener("click", function () {
navLinks.classList.remove("open");
mobileToggle.classList.remove("open");
mobileToggle.setAttribute("aria-expanded", "false");
});
});
// ---------- Active section highlight ----------
const sections = document.querySelectorAll("section[id]");
const navAnchors = navLinks.querySelectorAll("a");
function highlightNav() {
var scrollY = window.scrollY + 120;
sections.forEach(function (section) {
var top = section.offsetTop;
var height = section.offsetHeight;
var id = section.getAttribute("id");
if (scrollY >= top && scrollY < top + height) {
navAnchors.forEach(function (a) {
a.classList.remove("active");
if (a.getAttribute("href") === "#" + id) {
a.classList.add("active");
}
});
}
});
}
window.addEventListener("scroll", highlightNav, { passive: true });
highlightNav();
// ---------- Scroll reveal (IntersectionObserver) ----------
var revealSections = document.querySelectorAll(".reveal-section");
// On mobile, skip animation and show all sections immediately
if (window.innerWidth <= 768) {
revealSections.forEach(function (s) {
s.classList.add("revealed");
});
} else if ("IntersectionObserver" in window) {
var observer = new IntersectionObserver(
function (entries) {
entries.forEach(function (entry) {
if (entry.isIntersecting) {
entry.target.classList.add("revealed");
observer.unobserve(entry.target);
}
});
},
{ threshold: 0.1, rootMargin: "0px 0px -50px 0px" }
);
revealSections.forEach(function (section) {
observer.observe(section);
});
} else {
// Fallback: just show everything
revealSections.forEach(function (s) {
s.classList.add("revealed");
});
}
// ---------- Project filtering ----------
var filterButtons = document.querySelectorAll(".filter-btn");
var projectCards = document.querySelectorAll(".project-card");
filterButtons.forEach(function (btn) {
btn.addEventListener("click", function () {
var filter = this.getAttribute("data-filter");
filterButtons.forEach(function (b) {
b.classList.remove("active");
b.setAttribute("aria-selected", "false");
});
this.classList.add("active");
this.setAttribute("aria-selected", "true");
projectCards.forEach(function (card) {
var category = card.getAttribute("data-category");
if (filter === "all" || category === filter) {
card.classList.remove("hidden");
} else {
card.classList.add("hidden");
}
});
});
});
// ---------- Toast notifications ----------
var toastContainer = document.getElementById("toastContainer");
function showToast(message) {
var toast = document.createElement("div");
toast.className = "toast toast-enter";
toast.textContent = message;
toastContainer.appendChild(toast);
setTimeout(function () {
toast.classList.remove("toast-enter");
toast.classList.add("toast-exit");
setTimeout(function () {
if (toast.parentNode) toast.parentNode.removeChild(toast);
}, 300);
}, 2500);
}
// ---------- Navbar background on scroll ----------
var navbar = document.getElementById("navbar");
window.addEventListener(
"scroll",
function () {
if (window.scrollY > 20) {
navbar.style.borderBottomColor = "var(--border)";
} else {
navbar.style.borderBottomColor = "transparent";
}
},
{ passive: true }
);
})();