-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
82 lines (70 loc) · 2.38 KB
/
Copy pathscript.js
File metadata and controls
82 lines (70 loc) · 2.38 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
const openButton = document.querySelector("#openLetter");
const musicButton = document.querySelector("#musicToggle");
const audio = document.querySelector("#letterAudio");
const revealItems = document.querySelectorAll(".reveal");
const memoryCards = document.querySelectorAll(".memory-card");
const photoCards = document.querySelectorAll(".photo-card");
const MUSIC_START_SECONDS = 80;
openButton?.addEventListener("click", () => {
document.body.classList.add("letter-open");
document.querySelector("#letter")?.scrollIntoView({ behavior: "smooth", block: "start" });
});
musicButton?.addEventListener("click", async () => {
if (!audio) return;
try {
if (!audio.getAttribute("src")) {
audio.setAttribute("src", audio.dataset.src || "assets/audio/bushuo.mp3");
}
if (audio.paused) {
audio.currentTime = Math.min(MUSIC_START_SECONDS, Number.isFinite(audio.duration) ? audio.duration : MUSIC_START_SECONDS);
await audio.play();
musicButton.textContent = "暂停音乐";
musicButton.setAttribute("aria-pressed", "true");
} else {
audio.pause();
musicButton.textContent = "播放";
musicButton.setAttribute("aria-pressed", "false");
}
} catch {
musicButton.textContent = "播放";
}
});
audio?.addEventListener("ended", () => {
musicButton.textContent = "播放";
musicButton.setAttribute("aria-pressed", "false");
});
memoryCards.forEach((card) => {
card.addEventListener("click", () => {
card.classList.toggle("is-open");
});
});
photoCards.forEach((card) => {
const imagePath = card.dataset.image;
if (imagePath) {
const probe = new Image();
probe.onload = () => {
card.classList.add("has-image");
card.style.backgroundImage = `linear-gradient(180deg, rgba(7, 17, 31, 0.08), rgba(7, 17, 31, 0.5)), url("${imagePath}")`;
};
probe.src = imagePath;
}
card.addEventListener("click", () => {
card.classList.toggle("is-open");
});
});
if ("IntersectionObserver" in window) {
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
entry.target.classList.add("is-visible");
observer.unobserve(entry.target);
}
});
},
{ threshold: 0.12 }
);
revealItems.forEach((item) => observer.observe(item));
} else {
revealItems.forEach((item) => item.classList.add("is-visible"));
}