lottery-docs/_config/js/header-scroll.js
2025-10-11 23:27:40 +08:00

35 lines
1.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 等待 docsify 完成页面渲染后执行
window.$docsify.plugins = [].concat(window.$docsify.plugins || [], function(hook) {
hook.ready(function() {
let lastScrollTop = 0;
let scrolling = false;
// 创建一个获取 header 的函数
function getHeader() {
return document.querySelector('.header');
}
window.addEventListener('scroll', function() {
if (!scrolling) {
window.requestAnimationFrame(function() {
const header = getHeader();
if (!header) return; // 如果没有找到 header直接返回
let currentScrollTop = window.pageYOffset || document.documentElement.scrollTop;
if (currentScrollTop > lastScrollTop && currentScrollTop > 50) { // 添加一个最小滚动距离
// 向下滚动
header.style.transform = 'translateY(-100%)';
} else {
// 向上滚动
header.style.transform = 'translateY(0)';
}
lastScrollTop = currentScrollTop;
scrolling = false;
});
scrolling = true;
}
});
});
});