scroll2078

navigation bar behavior based on scroll bar

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greasyfork.org/scripts/491240/1352705/scroll2078.js

document.addEventListener('DOMContentLoaded', function () {
    const header = document.querySelector('.header');
    let lastScrollTop = 0;

    function handleScroll() {
        let scrollTop = window.pageYOffset || document.documentElement.scrollTop;

        if (scrollTop > lastScrollTop) {

            header.style.transform = 'translateY(-52px)';
        } else {
            if (scrollTop <= 18) {
                header.style.transform = `translateY(${scrollTop - 18}px)`;
            } else {
                header.style.transform = 'translateY(0px)';
            }
        }

        lastScrollTop = scrollTop <= 0 ? 0 : scrollTop;
    }

    window.addEventListener('scroll', handleScroll);

    header.style.transform = 'translateY(-16px)';
});