window.addEventListener('scroll', function(){ });
스크롤바를 조작하면 scroll 이벤트가 발생하며, 페이지를 스크롤할 때마다 원하는 코드를 실행할 수 있다. 스크롤이벤트는 관습적으로 window에 붙임!
window.addEventListener('scroll', function(){
console.log( window.scrollY )
});
💡 window.scrollY = window.pageYOffset
둘이 같은 기능이나 pageYOffset이 호환성이 더 좋음
// 위에서부터 100px 위치로 스크롤 해줌
window.scrollTo(0, 100)
// 현재위치에서 +100px 만큼 스크롤 해줌
window.scrollBy(0, 100)
💡scrollY는 window에만 붙일수 있다. div와 같은 일반 html 요소의 스크롤 높이를 귀하기 위해서는 scrollTop을 이용해야 한다.
//현재 웹페이지 스크롤양
document.querySelector('html').scrollTop;
//현재 웹페이지의 스크롤 가능한 실제높이
document.querySelector('html').scrollHeight;
//현재 웹페이지에서 보이는 높이
document.querySelector('html').clientHeight;
// document.querySelector('html') = document.documentElement
화면이 스무스하게 움직이는데 css 파일 상단에 :root { scroll-behavior : auto } 적어주면 스무스하게 움직이는 것을 막아준다.

function currentPercentage() {
let currY = document.documentElement.scrollTop;//스크롤한 높이
let totalY = document.documentElement.scrollHeight - document.documentElement.clientHeight;//스크롤 가능한 높이
let percentage = (currY / totalY) * 100;//퍼센트 값
document.querySelector(".purple-line").style.width = percentage + "%";
}
window.addEventListener('scroll', currentPercentage);
스크롤의 %값 구하기
스크롤된 양 (scrollTop) / 스크롤 가능한 높이 (scrollHeight - clientHeight) * 100