Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ramzs/92f81369ff9b79ce46e394cdc213268e to your computer and use it in GitHub Desktop.
Save ramzs/92f81369ff9b79ce46e394cdc213268e to your computer and use it in GitHub Desktop.
Фикс шапка. При прокрутке вниз уезжает а при прокрутке вверх выезжает
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