<nav id="nav" class="nav">
<ul class="nav__menu">
<li><a href="#one"class="nav__menu--foused">ONE</a></li>
<li><a href="#two">TWO</a></li>
<li><a href="#three">THREE</a></li>
<li><a href="#four">FOUR</a></li>
<div class="marker"></div>
</ul>
</nav>
<section id="one">one</section>
<section id="two">two</section>
<section id="three">three</section>
<section id="four">four</section>
html {
scroll-behavior: smooth;
}
a {
color:inherit;
text-decoration:none;
}
.nav {
position: fixed;
width: 100%;
background-color: white;
box-shadow: rgba(50, 50, 93, 0.25) 0px 8px 13px -5px,
rgba(0, 0, 0, 0.3) 0px 8px 16px -8px;
display: flex;
justify-content: center;
}
.nav__menu {
display: flex;
}
.nav__menu > li {
padding: 1.5rem;
}
.nav__menu--foused {
color: purple;
}
.marker {
content: "";
position: absolute;
bottom: 1.2rem;
left: 50%;
width: 0%;
height: 3px;
background-color: purple;
transition: 0.2s;
}
section {
text-align: center;
font-size: 20rem;
height: 200vh;
color: white;
}
#one {
background: palegoldenrod;
}
#two {
background: palegreen;
}
#three {
background: paleturquoise;
}
#four {
background: palevioletred;
}
const marker=document.querySelector(".marker");
//nav의 marker의 길이와 위치를 설정하는 함수
//A function to set the length and position of the nav marker.
function setMarker(e) {
marker.style.left = e.offsetLeft+"px";
marker.style.width = e.offsetWidth+"px";
}
const sections = document.querySelectorAll("section");
const menus = document.querySelectorAll(".nav__menu > li > a")
//스크롤 위치에 따라 해당하는 menu의 색깔과 마커가 달라짐
//The color and marker of the corresponding menu change according to the scroll position
window.addEventListener("scroll",()=>{
//현재 영역의 id값
//id of the current section
let current=""
sections.forEach(section=>{
//각 section의 top 위치(절대적 위치)
// The top of each section (absolute)
const sectionTop = window.scrollY + section.getBoundingClientRect().top;
//누적된 스크롤이 section의 top위치에 도달했거나 section의 안에 위치할 경우
//When the accumulated scroll reaches the top of the section or is located inside the section
if(window.scrollY >= sectionTop) {
current = section.getAttribute("id");
}
menus.forEach(menu=>{
menu.classList.remove("nav__menu--foused");
const href = menu.getAttribute("href").substring(1);
if(href===current) {
//현재 있는 영역의 id와 메뉴의 링크주소가 일치할때
//When the Id of the current section matches the href of the menu
menu.classList.add("nav__menu--foused");
setMarker(menu);
}
})
})
})
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.