scroll2078

navigation bar behavior based on scroll bar

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @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)';
});