Но теперь, когда я использую условное значение, когда страница обновляется, второй пункт меню освещается сначала, и пороговое количество, по -видимому, начинается со второго элемента меню. Если я укажу значение без условного оператора, все работает правильно. < /P>
код: < /p>
Код: Выделить всё
let threshold
const observer = new IntersectionObserver((entries) => {
if (window.innerHeight < 1080) {
threshold = [0.5, 1, 0.5, 0.5]
} else {
threshold = [0.5, 1, 0.9, 1]
}
console.log(window.innerHeight)
console.log(threshold)
entries.forEach((entry) => {
if (entry.isIntersecting) {
document.querySelectorAll('.header__nav-menu__list__item').forEach((link) => {
let id = link.getAttribute('href').replace('#', '')
if (id === entry.target.id) {
link.classList.add('header__nav-menu__list__item--active')
} else {
link.classList.remove('header__nav-menu__list__item--active')
}
})
}
})
}, {
rootMargin: "-100px 0px 0px 0px",
threshold: threshold
})
document.querySelectorAll('.block').forEach(block => { observer.observe(block) })
Подробнее здесь: https://stackoverflow.com/questions/797 ... th-another
Мобильная версия