mirror of
https://github.com/YouHaveTrouble/youhavetrouble.github.io.git
synced 2026-05-12 06:16:55 +00:00
fix mobile section observer
This commit is contained in:
@@ -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) => {
|
||||||
root: null,
|
if (window.innerWidth <= 767) return;
|
||||||
rootMargin: '-20% 0px -20% 0px',
|
|
||||||
threshold: 0.2
|
|
||||||
};
|
|
||||||
|
|
||||||
const observer = new IntersectionObserver((entries, observer) => {
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}, {
|
||||||
|
root: null,
|
||||||
|
rootMargin: '-20% 0px -20% 0px',
|
||||||
|
threshold: 0.3
|
||||||
|
});
|
||||||
|
|
||||||
|
const mobileObserver = new IntersectionObserver((entries) => {
|
||||||
|
if (window.innerWidth > 767) return;
|
||||||
|
entries.forEach(entry => {
|
||||||
|
if (entry.isIntersecting) {
|
||||||
|
const sectionId = entry.target.id;
|
||||||
|
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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user