-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
305 lines (262 loc) · 9.74 KB
/
Copy pathmain.js
File metadata and controls
305 lines (262 loc) · 9.74 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
/* ================================
MAIN.JS — Renewable Energy Website
Syauqi Nuzul Abdi · STITEK Bontang
================================ */
document.addEventListener("DOMContentLoaded", () => {
// ─── Read Progress Bar ───
const readProgress = document.getElementById("readProgress");
const updateProgress = () => {
const scrollTop = window.scrollY;
const docHeight =
document.documentElement.scrollHeight - window.innerHeight;
const pct = docHeight > 0 ? (scrollTop / docHeight) * 100 : 0;
if (readProgress) readProgress.style.width = pct + "%";
};
window.addEventListener("scroll", updateProgress, { passive: true });
// ─── Navbar scroll behavior ───
const navbar = document.getElementById("navbar");
const handleNavbar = () => {
if (navbar) {
if (window.scrollY > 60) {
navbar.classList.add("scrolled");
} else {
navbar.classList.remove("scrolled");
}
}
};
window.addEventListener("scroll", handleNavbar, { passive: true });
handleNavbar();
// ─── Hamburger / Mobile Menu ───
const hamburger = document.getElementById("hamburger");
const mobileMenu = document.getElementById("mobileMenu");
const mobileOverlay = document.getElementById("mobileOverlay");
const openMenu = () => {
hamburger?.classList.add("active");
mobileMenu?.classList.add("active");
mobileOverlay?.classList.add("active");
document.body.style.overflow = "hidden";
};
const closeMenu = () => {
hamburger?.classList.remove("active");
mobileMenu?.classList.remove("active");
mobileOverlay?.classList.remove("active");
document.body.style.overflow = "";
};
hamburger?.addEventListener("click", () => {
if (mobileMenu?.classList.contains("active")) {
closeMenu();
} else {
openMenu();
}
});
mobileOverlay?.addEventListener("click", closeMenu);
// Close menu on mobile link click
document.querySelectorAll(".mobile-menu__link").forEach((link) => {
link.addEventListener("click", closeMenu);
});
// ─── Scroll To Top ───
const scrollTopBtn = document.getElementById("scrollTop");
window.addEventListener(
"scroll",
() => {
if (scrollTopBtn) {
if (window.scrollY > 400) {
scrollTopBtn.classList.add("visible");
} else {
scrollTopBtn.classList.remove("visible");
}
}
},
{ passive: true },
);
scrollTopBtn?.addEventListener("click", () => {
window.scrollTo({ top: 0, behavior: "smooth" });
});
// ─── AOS Scroll Animations ───
const aosElements = document.querySelectorAll("[data-aos]");
const aosObserver = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const delay = entry.target.closest(
".cards-grid, .dampak-grid, .progress-grid",
)
? Array.from(entry.target.parentElement.children).indexOf(
entry.target,
) * 100
: 0;
setTimeout(() => {
entry.target.classList.add("aos-animate");
}, delay);
aosObserver.unobserve(entry.target);
}
});
},
{
threshold: 0.05,
rootMargin: "0px 0px -20px 0px",
},
);
aosElements.forEach((el) => aosObserver.observe(el));
// Fallback: paksa tampilkan semua elemen AOS yang belum muncul setelah 2.5 detik
// (untuk browser HP yang IntersectionObserver-nya bermasalah)
setTimeout(() => {
document.querySelectorAll("[data-aos]:not(.aos-animate)").forEach((el) => {
el.classList.add("aos-animate");
});
}, 2500);
// ─── Progress Bar Animations (count up) ───
const progressCards = document.querySelectorAll(".progress-card");
const progressObserver = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const fill = entry.target.querySelector(".progress-card__fill");
const valueEl = entry.target.querySelector(".progress-card__value");
if (fill) {
const target = parseInt(fill.dataset.target) || 70;
fill.style.width = target + "%";
}
if (valueEl) {
const countTarget = parseInt(valueEl.dataset.count) || 0;
const suffix = valueEl.dataset.suffix || "%";
animateCount(valueEl, 0, countTarget, 1600, suffix);
}
progressObserver.unobserve(entry.target);
}
});
},
{ threshold: 0.4 },
);
progressCards.forEach((card) => progressObserver.observe(card));
function animateCount(el, from, to, duration, suffix) {
const start = performance.now();
const isLargeNum = to > 1000;
const step = (now) => {
const elapsed = now - start;
const progress = Math.min(elapsed / duration, 1);
const eased = 1 - Math.pow(1 - progress, 3); // ease out cubic
const current = Math.round(from + (to - from) * eased);
if (suffix === "%") {
el.textContent = current + "%";
} else {
el.textContent = current.toLocaleString("id-ID") + suffix;
}
if (progress < 1) {
requestAnimationFrame(step);
}
};
requestAnimationFrame(step);
}
// ─── Split Earth Hover Interactivity ───
const globe = document.querySelector(".split-earth__globe");
if (globe) {
globe.addEventListener("mousemove", (e) => {
const rect = globe.getBoundingClientRect();
const x = (e.clientX - rect.left) / rect.width - 0.5;
const y = (e.clientY - rect.top) / rect.height - 0.5;
globe.style.transform = `perspective(600px) rotateY(${x * 8}deg) rotateX(${-y * 6}deg)`;
});
globe.addEventListener("mouseleave", () => {
globe.style.transform = "perspective(600px) rotateY(0) rotateX(0)";
globe.style.transition = "transform 0.5s ease";
});
globe.addEventListener("mouseenter", () => {
globe.style.transition = "transform 0.1s ease";
});
}
// ─── Smooth scroll for anchor links ───
document.querySelectorAll('a[href^="#"]').forEach((anchor) => {
anchor.addEventListener("click", (e) => {
const target = document.querySelector(anchor.getAttribute("href"));
if (target) {
e.preventDefault();
const offset = 72; // navbar height
const top =
target.getBoundingClientRect().top + window.scrollY - offset;
window.scrollTo({ top, behavior: "smooth" });
}
});
});
// ─── Active navbar link highlight ───
const sections = document.querySelectorAll("section[id]");
const navLinks = document.querySelectorAll(".navbar__link");
const sectionObserver = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
navLinks.forEach((link) => {
link.style.fontWeight =
link.getAttribute("href") === "#" + entry.target.id
? "700"
: "500";
link.style.color =
link.getAttribute("href") === "#" + entry.target.id
? "var(--green-dark)"
: "";
});
}
});
},
{ threshold: 0.4 },
);
sections.forEach((s) => sectionObserver.observe(s));
// ─── Card hover: subtle tilt ───
document
.querySelectorAll(".card, .dampak-item, .tech-node")
.forEach((card) => {
card.addEventListener("mousemove", (e) => {
const rect = card.getBoundingClientRect();
const x = (e.clientX - rect.left) / rect.width - 0.5;
const y = (e.clientY - rect.top) / rect.height - 0.5;
card.style.transform = `translateY(-4px) rotateX(${-y * 4}deg) rotateY(${x * 4}deg)`;
});
card.addEventListener("mouseleave", () => {
card.style.transform = "";
});
});
// ─── Lightbox Poster ───
const lightbox = document.getElementById("lightbox");
const lightboxClose = document.getElementById("lightboxClose");
const lightboxBackdrop = document.getElementById("lightboxBackdrop");
const posterZoomBtn = document.getElementById("posterZoomBtn");
const posterImg = document.getElementById("posterImg");
const openLightbox = () => {
lightbox?.classList.add("active");
document.body.style.overflow = "hidden";
};
const closeLightbox = () => {
lightbox?.classList.remove("active");
document.body.style.overflow = "";
};
posterZoomBtn?.addEventListener("click", openLightbox);
posterImg?.addEventListener("click", openLightbox);
lightboxClose?.addEventListener("click", closeLightbox);
lightboxBackdrop?.addEventListener("click", closeLightbox);
document.addEventListener("keydown", (e) => {
if (e.key === "Escape") closeLightbox();
});
// ─── Lightbox Layout ───
const lightboxLayout = document.getElementById("lightboxLayout");
const lightboxLayoutClose = document.getElementById("lightboxLayoutClose");
const lightboxLayoutBackdrop = document.getElementById(
"lightboxLayoutBackdrop",
);
const layoutZoomBtn = document.getElementById("layoutZoomBtn");
const layoutImg = document.getElementById("layoutImg");
const openLightboxLayout = () => {
lightboxLayout?.classList.add("active");
document.body.style.overflow = "hidden";
};
const closeLightboxLayout = () => {
lightboxLayout?.classList.remove("active");
document.body.style.overflow = "";
};
layoutZoomBtn?.addEventListener("click", openLightboxLayout);
layoutImg?.addEventListener("click", openLightboxLayout);
lightboxLayoutClose?.addEventListener("click", closeLightboxLayout);
lightboxLayoutBackdrop?.addEventListener("click", closeLightboxLayout);
console.log(
"🌿 Renewable Energy Website — Loaded. Syauqi Nuzul Abdi · STITEK Bontang",
);
});