Сообщество IT-специалистов
Ответы на любые вопросы об IT
Профессиональное развитие в IT
Удаленная работа для IT-специалистов
const blocks = document.querySelectorAll('li'); const debounce = (callback, delay) => { let timeout = null; return (...args) => { clearTimeout(timeout); timeout = setTimeout(callback, delay, ...args); }; }; const update = (entries) => { const sortedEntries = entries.sort((a, b) => b.intersectionRatio - a.intersectionRatio); const entry = sortedEntries[0].target; for (let block of blocks) { if (block === entry) { entry.classList.add('active'); } else { block.classList.remove('active'); } } }; const debouncedUpdate = debounce(update, 20); const observer = new IntersectionObserver(entries => { debouncedUpdate(entries); }, { threshold: [0.25] }); for (let block of blocks) { observer.observe(block); }