hexo-theme-keep/source/js/header-shrink.js

42 lines
1.5 KiB
JavaScript
Raw Normal View History

const pageTemplateDom = document.querySelector('.page-main-content');
2020-04-28 23:20:17 +08:00
const articleTocContainerDom = document.querySelector('.article-toc-container');
2020-04-05 08:32:44 +08:00
const headerDom = document.querySelector('.header-wrapper');
const menuBarDom = document.querySelector('.menu-bar');
const windowMaskDom = document.querySelector('.window-mask');
const scrollPercentDom = document.querySelector('.scroll-percent');
let isHeaderShrink = false;
const headerHeight = headerDom.getBoundingClientRect().height;
window.addEventListener('scroll', function (_e) {
const scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
if (!isHeaderShrink && scrollTop > headerHeight) {
isHeaderShrink = true;
headerDom.classList.add('header-wrapper-shrink');
pageTemplateDom.classList.add('page-main-content-top-shrink');
2020-04-28 23:20:17 +08:00
if (articleTocContainerDom) {
articleTocContainerDom.classList.add('article-toc-container-shrink');
}
2020-04-05 08:32:44 +08:00
} else if (isHeaderShrink && scrollTop <= headerHeight) {
isHeaderShrink = false;
headerDom.classList.remove('header-wrapper-shrink');
pageTemplateDom.classList.remove('page-main-content-top-shrink');
2020-04-28 23:20:17 +08:00
if (articleTocContainerDom) {
articleTocContainerDom.classList.remove('article-toc-container-shrink');
}
2020-04-05 08:32:44 +08:00
}
});
menuBarDom.addEventListener('click', function (_e) {
headerDom.classList.toggle('header-drawer-show');
});
windowMaskDom.addEventListener('click', function (_e) {
headerDom.classList.toggle('header-drawer-show');
});