Код: Выделить всё
function throttle(fn, delay) {
let timer = null
return function(...args) {
if (timer == null) {
timer = setTimeout(() => {
fn.apply(this, args)
clearTimeout(timer)
timer = null
}, delay)
}
}
}
function handleScroll() {
if (window.innerWidth >= 1024) {
const currentScroll = window.scrollY
console.log('currentScroll' + '----------------------------------' + currentScroll)
if (currentScroll === 0) {
stickyHeader.style.top = '40px'
announcementBar.style.top = '0'
blogHeader.style.top = '100px'
} else if (currentScroll > lastScroll) {
blogHeader.style.top = '0px'
stickyHeader.style.top = '-60px'
announcementBar.style.top = '-40px'
} else if (currentScroll < lastScroll) {
blogHeader.style.top = '60px'
stickyHeader.style.top = '0px'
}
lastScroll = currentScroll;
}
}
window.addEventListener("scroll", throttle(handleScroll, 250), {
passive: true
})< /code>
div { border:1px solid black; height:300px; }< /code>
Div 1
Div 2
Div 3Я надеюсь, что когда я прокручиваю к вершине, я могу получить окно.>
Подробнее здесь: https://stackoverflow.com/questions/796 ... ly-to-be-0
Мобильная версия