这是全局变量
const navigation = document.getElementById('nav_list');
const sections = document.querySelectorAll('section');
const links = document.querySelectorAll('li')
let navLinks = '';
该函数用于创建导航。
const navMaker = function() {
sections.forEach(section => {
const sectId = section.id;
const sectNav = section.dataset.name;
navLinks = navLinks + `<li><a class="links_menu" href="#${sectId}">${sectNav}</a>
</li>`;
});
navigation.innerHTML = navLinks;
}
navMaker();
该函数用于添加和删除活动类
这个常量用于访问所有列表:
const linkErrays = document.querySelectorAll('li');
const level = (section) => {
return Math.floor(section.getBoundingClientRect().top)
}
const delateClass = (section) => {
section.classList.remove('active_section');
}
const addClass = (condition, section) => {
if (condition) {
section.classList.add('active_section');
}
}
这是当有人滚动时的类激活
const classActivation = () => {
linkErrays.forEach(section => {
const elementLevel = level(section);
inviewport = () => elementLevel < 50 && elementLevel >= -650
delateClass(section);
addClass(inviewport(), section);
});
}
window.addEventListener('scroll', classActivation);
繁星coding
HUH函数
相关分类