Created
July 31, 2022 09:41
-
-
Save ramzs/92f81369ff9b79ce46e394cdc213268e to your computer and use it in GitHub Desktop.
Фикс шапка. При прокрутке вниз уезжает а при прокрутке вверх выезжает
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let lastScrollTop = 0; | |
let header = document.querySelector('.header'); | |
let headerHeight = header.scrollHeight; | |
let wrapper = document.querySelector('.wrapper'); | |
wrapper.style.paddingTop = headerHeight + 'px'; | |
let headerPos = function () { | |
let top = window.pageYOffset; | |
let scrollTop = window.pageYOffset || document.documentElement.scrollTop; | |
if (lastScrollTop > headerHeight) { | |
header.classList.add('fixed'); | |
} | |
if (lastScrollTop > top) { | |
header.classList.add('scrollUp'); | |
header.classList.remove('scrollDown'); | |
} else if (scrollTop > headerHeight) { | |
header.classList.remove('scrollUp'); | |
header.classList.add('scrollDown'); | |
} | |
if (scrollTop == 0) { | |
header.classList.remove('scrollUp'); | |
header.classList.remove('scrollDown'); | |
header.classList.remove('fixed'); | |
} | |
lastScrollTop = top; | |
}; | |
window.addEventListener('load', headerPos); | |
window.addEventListener('scroll', headerPos); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment