uliyanov_maks
@uliyanov_maks
Новичок

Не могу разобраться, при скролле не добавляется/удаляется класс. Что не так?

Подскажите пожалуйста, что не так с этим скриптом?

при скролле не добавляется/удаляется класс - scroll-up

const targetNavbarDesktop = document.querySelector('.fixed-navbar-desktop');
const targetNavbarMobile = document.querySelector('.fixed-navbar-mobile');
const fixedNavbarDesktop = document.querySelector('.navbar');
const fixedNavbarMobile = document.querySelector('.navbar');
const obFixedNavbar = new IntersectionObserver(obCallback);

let lastScrollPosition = 0;

function obCallback(payload) {
	const currentScrollPosition = window.pageYOffset;
	const isScrollingUp = currentScrollPosition < lastScrollPosition;

	if (document.documentElement.clientWidth > 992) {
		if (payload[0].boundingClientRect.y < 0) {
			document.querySelector('.header').style.marginBottom = fixedNavbarDesktop.offsetHeight + 'px';
			fixedNavbarDesktop.classList.add('navbar_fixed');
		} else {
			fixedNavbarDesktop.classList.remove('navbar_fixed');
			document.querySelector('.header').style.marginBottom = '0';
		}
	} else {
		if (payload[0].boundingClientRect.y < 0) {
			document.querySelector('.header').style.marginBottom = fixedNavbarMobile.offsetHeight + 'px';
			fixedNavbarMobile.classList.add('navbar_fixed');
			if (isScrollingUp) {
				fixedNavbarMobile.classList.add('scroll-up'); // Появится при скролле вверх
			} else {
				fixedNavbarMobile.classList.remove('scroll-up'); // Удалится при скролле вниз
			}
		} else {
			document.querySelector('.header').style.marginBottom = '0';
			fixedNavbarMobile.classList.remove('navbar_fixed');
			fixedNavbarMobile.classList.remove('scroll-up');
		}
	}

	lastScrollPosition = currentScrollPosition;
}

if (document.documentElement.clientWidth > 992) {
	obFixedNavbar.observe(targetNavbarDesktop);
} else {
	obFixedNavbar.observe(targetNavbarMobile);
}
  • Вопрос задан
  • 91 просмотр
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы