fix mobile section observer

This commit is contained in:
2023-12-28 23:51:51 +01:00
parent a037f75f1d
commit 618d2699c5
+23 -11
View File
@@ -1,25 +1,37 @@
const sections = document.querySelectorAll('main section'); const sections = document.querySelectorAll('main section');
const navLinks = document.querySelectorAll('[data-link]'); const navLinks = document.querySelectorAll('[data-link]');
const options = { const observer = new IntersectionObserver((entries) => {
if (window.innerWidth <= 767) return;
entries.forEach(entry => {
if (entry.isIntersecting) {
const sectionId = entry.target.id;
setActiveLink(sectionId);
}
});
}, {
root: null, root: null,
rootMargin: '-20% 0px -20% 0px', rootMargin: '-20% 0px -20% 0px',
threshold: 0.2 threshold: 0.3
}; });
const observer = new IntersectionObserver((entries, observer) => { const mobileObserver = new IntersectionObserver((entries) => {
if (window.innerWidth > 767) return;
entries.forEach(entry => { entries.forEach(entry => {
entries.forEach(entry => { if (entry.isIntersecting) {
if (entry.isIntersecting) { const sectionId = entry.target.id;
const sectionId = entry.target.id; setActiveLink(sectionId);
setActiveLink(sectionId); }
}
});
}); });
}, options); }, {
root: null,
rootMargin: '-30% 0px -30% 0px',
threshold: 0.1
});
sections.forEach(section => { sections.forEach(section => {
observer.observe(section); observer.observe(section);
mobileObserver.observe(section);
}); });
function setActiveLink(sectionId) { function setActiveLink(sectionId) {