Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
239 lines (212 loc) · 8.48 KB
/
Copy pathapp.js
File metadata and controls
239 lines (212 loc) · 8.48 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
// const sections = document.querySelectorAll("section");
// // Add a scroll event listener to the window object
// window.addEventListener("scroll", () => {
// // Loop through each section element in the 'sections' array/NodeList
// sections.forEach((section) => {
// // Get the current position and dimensions of the section relative to the viewport
// const change = section.getBoundingClientRect();
console.log(change);
// // Check if the section is in the "active" viewing area:
// // 1. Top of section is above the middle of the viewport (window.innerHeight / 2)
// // 2. Bottom of section is below the middle of the viewport
// if (
// change.top <= window.innerHeight / 2 &&
// change.bottom >= window.innerHeight / 2
// ) {
// // When section is in view, change the body background color
// // using the color value stored in the section's data-color attribute
// document.body.style.backgroundColor = section.dataset.color;
// }
// });
// });
// Select all <section> elements on the page
const sections = document.querySelectorAll("section");
// Select all <a> elements with class 'buy' or 'store'
const fontColor = document.querySelectorAll("a.buy, a.store");
// Helper function to check if a section is centered in the viewport
function isSectionCentered(section) {
const rect = section.getBoundingClientRect();
const middle = window.innerHeight / 2;
// Returns true if the section's top is above the middle and bottom is below the middle
return rect.top <= middle && rect.bottom >= middle;
}
function isSectionCenteredHash(section) {
const make = section.getBoundingClientRect();
const middle = window.innerHeight / 2;
// return statement
return make.top >= 0 && make.top <= middle;
}
// function isSectionCenteredHash(section) {
// const rect = section.getBoundingClientRect();
// const middle = window.innerHeight / 2;
// return rect.top >= 0 && rect.top <= middle;
// }
// Create an IntersectionObserver to watch when sections enter/leave the viewport
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
// If the section is intersecting and centered, change background color
if (entry.isIntersecting && isSectionCentered(entry.target)) {
document.body.style.backgroundColor = entry.target.dataset.color;
// If the section is 'issue-6', change font color for specific links
if (entry.target.id === "issue-6") {
fontColor.forEach((el) => {
el.style.color = "#e568ac";
});
}
} else if (entry.target.id === "issue-6") {
// If 'issue-6' is not centered, reset font color
fontColor.forEach((el) => {
el.style.color = "";
});
}
// if (entry.isIntersecting && isSectionCenteredHash(entry.target)) {
// // Update the URL hash when the section is intersect
// history.replaceState(null, null, `#${entry.target.id}`);
// }
});
},
{
root: null, // Use viewport as root
threshold: [0, 0.1, 0.5, 0.9, 1.0], // Trigger at multiple visibility levels
}
);
// Observe each section for intersection changes
sections.forEach((section) => observer.observe(section));
// const sectionsix = document.querySelector("section#issue-6");
// const fontColor = document.querySelectorAll("a.buy, a.store");
// window.addEventListener("scroll", () => {
// if (!sectionsix) return;
// const changea = sectionsix.getBoundingClientRect();
// if (
// changea.top <= window.innerHeight / 2 &&
// changea.bottom >= window.innerHeight / 2
// ) {
// fontColor.forEach((el) => {
// el.style.color = "#e568ac";
// });
// } else {
// fontColor.forEach((el) => {
// el.style.color = "";
// });
// }
// });
//Listen for scroll events to update the URL hash based on the section in view
// window.addEventListener("scroll", () => {
// sections.forEach((section) => {
// const rect = section.getBoundingClientRect();
// // If the section's top is in the upper half of the viewport, update the URL hash
// if (rect.top >= 0 && rect.top < window.innerHeight / 2) {
// history.replaceState(null, null, `/#${section.id}`);
// }
// });
// });
// Create a new IntersectionObserver to update the URL hash based on the section in view.
// // This is more performant than using a scroll event listener.
// Use IntersectionObserver for hash updates
// ...existing code...
// Remove or comment out the scroll event listener that updates the hash
// window.addEventListener("scroll", ...);
// Use IntersectionObserver for hash updates
const hashObserver = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
history.replaceState(null, null, `/#${entry.target.id}`);
}
});
},
{
root: null,
rootMargin: "0px 0px -50% 0px", // Top half of viewport
threshold: 0,
}
);
sections.forEach((section) => hashObserver.observe(section));
const navLinks = document.querySelectorAll("li a");
const hover = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting && isSectionCentered(entry.target)) {
const currentSection = entry.target.id;
// Reset all links first
navLinks.forEach((link) => {
link.style.fontWeight = "";
});
// Then set the appropriate color based on the section
if (currentSection === "issue-4") {
navLinks[4].style.fontWeight = "bold";
} else if (currentSection === "issue-3") {
navLinks[5].style.fontWeight = "bold";
} else if (currentSection === "issue-2") {
navLinks[6].style.fontWeight = "bold";
} else if (currentSection === "issue-1") {
navLinks[7].style.fontWeight = "bold";
}
}
});
},
{
root: null,
threshold: [0.5],
rootMargin: "0px",
}
);
// Make sure to observe all sections
sections.forEach((section) => hover.observe(section));
document.querySelectorAll("li a").forEach((link) => {
link.addEventListener("click", function (event) {
console.log("Link clicked:", this.getAttribute("href")); // Debug log
event.preventDefault();
const targetId = this.getAttribute("href").substring(1);
const targetElement = document.getElementById(targetId);
if (targetElement) {
targetElement.scrollIntoView({ behavior: "smooth" });
history.replaceState(null, null, `#${targetId}`); // Update hash right away
targetElement.setAttribute("tabindex", "-1"); // Make focusable if not already
// targetElement.focus({ preventScroll: true }); // Move focus for accessibility
}
});
});
// document.querySelectorAll("li a").forEach((link) => {
// link.addEventListener("click", function (event) {
// console.log("Link clicked:", this.getAttribute("href")); // Debug log
// event.preventDefault();
// const targetId = this.getAttribute("href").substring(1);
// const targetElement = document.getElementById(targetId);
// if (targetElement) {
// targetElement.scrollIntoView({ behavior: "smooth" });
// history.replaceState(null, null, `#${targetId}`);
// targetElement.setAttribute("tabindex", "-1");
// targetElement.focus({ preventScroll: true });
// } else {
// console.log("Target element not found:", targetId); // Debug log
// }
// });
// // });
// document.addEventListener("DOMContentLoaded", () => {
// document.querySelectorAll("li a").forEach((link) => {
// link.addEventListener("click", function (event) {
// event.preventDefault();
// const targetId = this.getAttribute("href").substring(1);
// const targetElement = document.getElementById(targetId);
// if (targetElement) {
// targetElement.scrollIntoView({ behavior: "smooth" });
// history.replaceState(null, null, `#${targetId}`);
// targetElement.setAttribute("tabindex", "-1");
// targetElement.focus({ preventScroll: true });
// targetElement.style.scrollMarginTop = "100px"; // Adjust scroll margin if needed
// }
// });
// });
// });
// document.querySelectorAll(".nav a").forEach((link) => {
// link.addEventListener("click", function (event) {
// event.preventDefault(); // Prevent default jump
// const targetId = this.getAttribute("href").substring(1);
// const targetElement = document.getElementById(targetId);
// if (targetElement) {
// targetElement.scrollIntoView({ behavior: "smooth" });
// }
// });
// });