Skip to content

Commit

Permalink
🐞 fix: 修复 sidebar 在切换单双栏后抖动
Browse files Browse the repository at this point in the history
  • Loading branch information
白云苍狗 committed Oct 18, 2023
1 parent b66674b commit 960697e
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/hexo-theme-async-ts/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ export function InitScroll() {
backtop && (backtop.style.backgroundSize = `100% ${ratio}%`);

const sidebarFun = scrollTop >= 70 ? 'add' : 'remove';
sidebar && sidebar.classList[sidebarFun]('fixed');
if (sidebar) {
sidebar.classList[sidebarFun]('fixed');
setSidebarWidth();
}

banner && (banner.style.transform = `matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, ${Math.min(scrollTop / 3, 100)}, 0, 1)`);

Expand All @@ -162,7 +165,7 @@ export function InitScroll() {

const setSidebarWidth = function () {
if (sidebar) {
sidebar.style.width = window.innerWidth > 992 ? `${sidebar.parentElement.clientWidth - 40}px` : 'auto';
sidebar.style.width = window.innerWidth > 992 && sidebar.classList.contains('fixed') ? `${sidebar.parentElement.clientWidth - 40}px` : 'auto';
}
};

Expand All @@ -171,13 +174,18 @@ export function InitScroll() {
container && window.scrollTo({ top: container.clientHeight - 20, behavior: 'smooth' });
};

const observer = new MutationObserver(() => {
setSidebarWidth()
});

const init = () => {
const sections = utils.qa('.trm-scroll-animation');
sections.forEach(element => {
element && intersectionObserver.observe(element);
});
scroll_fun();
setSidebarWidth();
observer.observe(document.body, { attributeFilter: ['style', 'class'] });
};

init();
Expand All @@ -193,6 +201,7 @@ export function InitScroll() {
window.removeEventListener('scroll', scroll_fun);
window.removeEventListener('resize', setSidebarWidth);
back_fun();
observer.disconnect();
});
}

Expand Down

0 comments on commit 960697e

Please sign in to comment.