Skip to content

Commit

Permalink
サイドバーを自動スクロール
Browse files Browse the repository at this point in the history
  • Loading branch information
yuma140902 committed Dec 20, 2023
1 parent 05732bc commit e4d01df
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,19 @@ function handleSidebar() {
else {
closeSidebar(sidebar);
}
})
});

if (sidebar_open) {
openSidebar(sidebar);
}
else {
closeSidebar(sidebar);
}

const current_page_item = document.querySelector('.sidebar .here');
if (!isVisible(current_page_item)) {
current_page_item.scrollIntoView({ behavior: 'instant' });
}
}

function openSidebar(sidebar) {
Expand All @@ -130,3 +136,9 @@ function closeSidebar(sidebar) {
function isMobile() {
return window.matchMedia('(max-width: 768px)').matches;
}

function isVisible(element) {
var rect = element.getBoundingClientRect();
var viewHeight = Math.max(document.documentElement.clientHeight, window.innerHeight);
return !(rect.bottom < 0 || rect.top - viewHeight >= 0);
}

0 comments on commit e4d01df

Please sign in to comment.