Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
66 lines (57 loc) · 2.49 KB
/
Copy pathscript.js
File metadata and controls
66 lines (57 loc) · 2.49 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
// ---------- Pre-loader ----------
window.addEventListener("load", () => {
setTimeout(() => document.body.classList.remove("is-preload"), 120);
});
setTimeout(() => document.body.classList.remove("is-preload"), 3500);
const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
// ---------- Reveal cards as they enter the viewport ----------
const cards = document.querySelectorAll(".card");
if (!reduceMotion && "IntersectionObserver" in window) {
const io = new IntersectionObserver(entries => {
entries.forEach(e => {
if (e.isIntersecting) {
e.target.classList.add("in-view");
io.unobserve(e.target); // reveal once, like Apple product pages
}
});
}, { threshold: 0.25 });
cards.forEach(c => io.observe(c));
} else {
cards.forEach(c => c.classList.add("in-view"));
}
// ---------- Parallax (rAF-driven, GPU-friendly transforms only) ----------
const heroContent = document.getElementById("heroContent");
const drifts = document.querySelectorAll(".drift");
let ticking = false;
function parallax() {
ticking = false;
const y = window.scrollY;
const vh = window.innerHeight;
// Hero: recedes upward at ~35% of scroll speed and fades — the classic
// Apple hero hand-off. (Background is fixed, so it forms a third layer.)
const p = Math.min(y / vh, 1);
heroContent.style.transform = `translateY(${y * 0.35}px) scale(${1 - p * 0.05})`;
heroContent.style.opacity = 1 - p * 1.15;
// Section content drifts gently against scroll direction relative to
// its distance from the viewport centre — subtle depth, never dizzy.
drifts.forEach(d => {
const r = d.getBoundingClientRect();
const offset = (r.top + r.height / 2 - vh / 2) / vh; // -1..1 around centre
d.style.transform = `translateY(${offset * -28}px)`;
});
// Slim nav appears after ~70% of the first viewport
document.body.classList.toggle("scrolled", y > vh * 0.7);
}
function onScroll() {
if (!ticking) { ticking = true; requestAnimationFrame(parallax); }
}
if (!reduceMotion) {
window.addEventListener("scroll", onScroll, { passive: true });
window.addEventListener("resize", onScroll, { passive: true });
parallax();
} else {
// Still show the slim nav without any motion effects
window.addEventListener("scroll", () => {
document.body.classList.toggle("scrolled", window.scrollY > window.innerHeight * 0.7);
}, { passive: true });
}