mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-03 18:02:49 +08:00
Stabilize landing navigation scroll state
This commit is contained in:
1 parent
ccceacba07
commit
6bdd0fa171
1 file changed
+15
-8
+15
-8
@@ -135,14 +135,21 @@ function Landing() {
|
|||||||
const sections = ["features", "architecture", "install"]
|
const sections = ["features", "architecture", "install"]
|
||||||
.map(id => document.getElementById(id))
|
.map(id => document.getElementById(id))
|
||||||
.filter((section): section is HTMLElement => section !== null);
|
.filter((section): section is HTMLElement => section !== null);
|
||||||
const observer = new IntersectionObserver(
|
const updateActiveSection = () => {
|
||||||
entries => entries.forEach(entry => {
|
const marker = window.innerHeight * 0.34;
|
||||||
if (entry.isIntersecting) setActiveNav(entry.target.id);
|
let current = "";
|
||||||
}),
|
sections.forEach(section => {
|
||||||
{ rootMargin: "-28% 0px -58%", threshold: 0 },
|
if (section.getBoundingClientRect().top <= marker) current = section.id;
|
||||||
);
|
});
|
||||||
sections.forEach(section => observer.observe(section));
|
setActiveNav(current);
|
||||||
return () => observer.disconnect();
|
};
|
||||||
|
updateActiveSection();
|
||||||
|
window.addEventListener("scroll", updateActiveSection, { passive: true });
|
||||||
|
window.addEventListener("resize", updateActiveSection);
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener("scroll", updateActiveSection);
|
||||||
|
window.removeEventListener("resize", updateActiveSection);
|
||||||
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return <div className="landing">
|
return <div className="landing">
|
||||||
|
|||||||
Reference in new issue
Block a user