Compare commits

...

6 Commits

Author SHA1 Message Date
XUE
d129f7caca
Merge 06eb8a66a12c523297d23a06b376889767a8e80d into bc778ee243e2a4a22ee5413adffde5baf79c079d 2024-12-16 22:53:32 +00:00
Attila Greguss
bc778ee243
fix: only display footer switches section if it actually has content (#517)
Some checks failed
Deploy Hugo site to Pages / build (push) Has been cancelled
Deploy Hugo site to Pages / deploy (push) Has been cancelled
2024-12-16 22:21:15 +00:00
XUE
06eb8a66a1 fix: reduce the frequency of jittering effect 2024-11-16 17:44:20 +08:00
XUE
081ad8b84f
Merge pull request #1 from imfing/main
update the theme 241104
2024-11-04 10:49:51 +08:00
XUE
7593ef4ae4 fix: add the debouncing and reduce the duplicate writes 2024-11-04 10:29:22 +08:00
XUE
b70d729283 fix: restore the sidebar position to make the selected entry showed 2024-11-03 21:47:31 +08:00
2 changed files with 50 additions and 20 deletions

View File

@ -1,8 +1,43 @@
document.addEventListener("DOMContentLoaded", function () {
scrollToActiveItem();
enableCollapsibles();
initializeSidebar();
});
function initializeSidebar() {
const sidebarScrollbar = document.querySelector("aside.sidebar-container > .hextra-scrollbar");
if (!sidebarScrollbar) return;
enableCollapsibles();
restoreSidebarPosition(sidebarScrollbar);
const debouncedSave = debounce((position) => {
saveSidebarPosition(position);
}, 150);
sidebarScrollbar.addEventListener('scroll', function() {
debouncedSave(this.scrollTop);
});
document.querySelectorAll('a').forEach(link => {
if (link.hostname === window.location.hostname) {
link.addEventListener('click', function(e) {
saveSidebarPosition(sidebarScrollbar.scrollTop);
});
}
});
}
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
function enableCollapsibles() {
const buttons = document.querySelectorAll(".hextra-sidebar-collapsible-button");
buttons.forEach(function (button) {
@ -16,21 +51,16 @@ function enableCollapsibles() {
});
}
function scrollToActiveItem() {
const sidebarScrollbar = document.querySelector("aside.sidebar-container > .hextra-scrollbar");
const activeItems = document.querySelectorAll(".sidebar-active-item");
const visibleActiveItem = Array.from(activeItems).find(function (activeItem) {
return activeItem.getBoundingClientRect().height > 0;
});
if (!visibleActiveItem) {
return;
}
const yOffset = visibleActiveItem.clientHeight;
const yDistance = visibleActiveItem.getBoundingClientRect().top - sidebarScrollbar.getBoundingClientRect().top;
sidebarScrollbar.scrollTo({
behavior: "instant",
top: yDistance - yOffset
});
function saveSidebarPosition(scrollPosition) {
localStorage.setItem('sidebarScrollPosition', scrollPosition);
}
function restoreSidebarPosition(sidebarScrollbar) {
const savedPosition = localStorage.getItem('sidebarScrollPosition');
if (savedPosition !== null) {
requestAnimationFrame(() => {
sidebarScrollbar.scrollTop = parseInt(savedPosition);
});
}
}

View File

@ -15,7 +15,7 @@
<footer class="hextra-footer hx-bg-gray-100 hx-pb-[env(safe-area-inset-bottom)] dark:hx-bg-neutral-900 print:hx-bg-transparent">
{{- if $enableFooterSwitches -}}
{{- if and $enableFooterSwitches (or hugo.IsMultilingual $displayThemeToggle) -}}
<div class="hx-mx-auto hx-flex hx-gap-2 hx-py-2 hx-px-4 {{ $footerWidth }}">
{{- partial "language-switch.html" (dict "context" .) -}}
{{- with $displayThemeToggle }}{{ partial "theme-toggle.html" }}{{ end -}}