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)
toggle() 메서드는 추가/제거를 하는 기능을 하기 때문에 해당 코드에서는 scroll을 할 때마다 toggle이 된다. 그렇기 때문에 위와 같은 현상이 나타났다.
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)