Detect Scroll Position
문제

목적

설계
변수 설정
방법
처음에 작성한 코드
문제점 발견
문제
해결
add()/remove() 사용하기
개선
Last updated


Last updated
const SCROLL_SHOW_STATE_CLASSMNAME = 'scrolling--show'
const fixedHeaderNode = document.querySelector('.header--fixed')const SCROLL_SHOW_STATE_CLASSMNAME = 'scrolling--show'
const fixedHeaderNode = document.querySelector('.header--fixed')
const scrollShowHide = function (target, Ypos = 0) {
if (window.scrollY >= Ypos) {
target.classList.toggle(SCROLL_SHOW_STATE_CLASSMNAME)
}
}
const handleScrollShowHide = function () {
scrollShowHide(fixedHeaderNode, 30)
}
window.addEventListener('scroll', handleScrollShowHide)const scrollShowHide = function (target, Ypos = 0) {
if (window.scrollY > Ypos) {
target.classList.add(SCROLL_SHOW_STATE_CLASSMNAME)
} else {
target.classList.remove(SCROLL_SHOW_STATE_CLASSMNAME)
}
}const SCROLL_SHOW_STATE_CLASSMNAME = 'scrolling--show'
const fixedHeaderNode = document.querySelector('.header--fixed')
var scrollShow = function (target, Ypos = 0, className) {
if (window.scrollY >= Ypos && !target.classList.contains(className)) {
target.classList.add(className)
}
}
const scrollHide = function (target, Ypos = 0, className) {
if (window.scrollY < Ypos && target.classList.contains(className)) {
target.classList.remove(className)
}
}
const handleScrollShowHide = function () {
scrollShow(fixedHeaderNode, 30, SCROLL_SHOW_STATE_CLASSNAME)
scrollHide(fixedHeaderNode, 30, SCROLL_SHOW_STATE_CLASSNAME)
}
window.addEventListener('scroll', handleScrollShowHide)