ScrollIntoView() : Navigation 클릭하면 해당 Section으로 이동

오늘의 정리 1. Navigation을 클릭하면 해당 Section으로 부드럽게 이동하는 기능 구현 2. Coding HTML (먼저 HTML에서 Navigation에 dataset을 이용해 이동할 section name을 넣어놓는다.) JS nav__menu.addEventListener(“click”, (event) => { const target = event.target; const link = target.dataset.link; const elem = document.querySelector(link); elem.scrollIntoView({ behavior: “smooth” }); }); 3. 설명 navbar에 click event를더하고, event parameter의 target을 이용해 클릭한 […]

Read more
Header scroll-animation : 스크롤 내리면 Background 색상 변경

오늘의 정리 1. 스크롤을 내릴 때 transparent => 색상이 변하는 기능을 구현하기 2. Coding const header = document.querySelector(“#header”); const headerHeight = header.getBoundingClientRect().height; window.addEventListener(“scroll”, () => { if (window.scrollY > headerHeight) { header.setAttribute(“style”, “background: white;”); } else { header.setAttribute(“style”, “background: transparent;”); } }); 3. 설명 해당 DOM의 높이를 변수로 잡는다. – getBoundingClientRect().height; window에 scroll 이벤트를 더한 […]

Read more