Skip to content

Commit

Permalink
chore: tweak mobile nav
Browse files Browse the repository at this point in the history
  • Loading branch information
Candinya committed May 31, 2024
1 parent 6a50d2f commit a1ce002
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 34 deletions.
67 changes: 35 additions & 32 deletions src/js/kr-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ window.copyCode =
const url = (window.kr?.siteRoot || "/") + "config.json";
return await fetch(url).then((res) => res.json());
};
const initPpageScrollDown = () => {
const initPageScrollDown = () => {
let isScrolledDown = false;
const pageScrollDownClass = () => {
window.requestAnimationFrame(() => {
Expand Down Expand Up @@ -64,56 +64,59 @@ window.copyCode =
menuWrapClone.setAttribute("id", "offcanvas-menu");
menuWrapClone.querySelectorAll("ul").forEach((el) => {
el.removeAttribute("id");
el.classList.add("ul-me");
});
menuWrapClone.style.height = "calc(100% - 60px)";
menuWrapClone.style.right = "-240px";

const navToggleWrapper = document.getElementById(
"kratos-nav-toggle-wrapper",
);

document
.getElementById("kratos-nav-toggle")
.addEventListener("click", () => {
const navToggleWrapper = document.getElementById(
"kratos-nav-toggle-wrapper",
);
if (navToggleWrapper.classList.contains("toon")) {
navToggleWrapper.classList.remove("toon");
menuWrapClone.style.right = "-240px";
menuWrapClone.classList.remove("show");
} else {
navToggleWrapper.classList.add("toon");
menuWrapClone.style.right = "0px";
menuWrapClone.classList.add("show");
}
});

// 在链接被点击时自动关闭
menuWrapClone.addEventListener("click", (e) => {
if (
e.target.tagName.toLowerCase() === "a" &&
e.target.getAttribute("href")
) {
navToggleWrapper.classList.remove("toon");
menuWrapClone.classList.remove("show");
}
});

document.getElementById("kratos-page").prepend(menuWrapClone);

// 在页面放大到超过断点时自动关闭
window.addEventListener("resize", (e) => {
const navToggleWrapper = document.getElementById(
"kratos-nav-toggle-wrapper",
);

if (window.innerWidth > 768) {
// 关闭
if (navToggleWrapper.classList.contains("toon")) {
navToggleWrapper.classList.remove("toon");
menuWrapClone.style.right = "-240px";
menuWrapClone.classList.remove("show");
}
}
});
};
const collapseBoxControl = () => {
const collapseBoxes = document
.querySelectorAll(".collapse-box-control")
.forEach((node) => {
node
.querySelector(".collapse-box-header")
?.addEventListener("click", (e) => {
node.classList.toggle("active");
});
});
const initCollapseBoxControl = () => {
document.querySelectorAll(".collapse-box-control").forEach((node) => {
node
.querySelector(".collapse-box-header")
?.addEventListener("click", (e) => {
node.classList.toggle("active");
});
});
};

const viewerJsInit = (kr) => {
const initViewerJs = (kr) => {
if (kr.enable) {
const postEntry = document.getElementsByClassName("kratos-page-content");
if (postEntry.length > 0) {
Expand Down Expand Up @@ -238,7 +241,7 @@ window.copyCode =
}, 1000);
};

const codeCopyInit = () => {
const initCodeCopy = () => {
const codeFigures = document.querySelectorAll("figure.highlight");
codeFigures.forEach((figure, count) => {
figure
Expand Down Expand Up @@ -361,7 +364,7 @@ window.copyCode =
};
};

const tocWidgetAnimInit = () => {
const initTocWidgetAnim = () => {
const sidebarTocWidget = document.getElementById("krw-toc");
const initFunc = () => {
// 有toc的页面
Expand Down Expand Up @@ -640,17 +643,17 @@ window.copyCode =
};

const initPerPage = () => {
tocWidgetAnimInit();
initTocWidgetAnim();
saveTitle();
codeCopyInit();
collapseBoxControl();
initCodeCopy();
initCollapseBoxControl();
commentsLazyLoad();
};

const initPerPageWithConfig = (kr) => {
initCopyrightNotice(kr.copyrightNoticeForCopy);
checkExpireNotify(kr.expireNotify);
viewerJsInit(kr.viewerjs);
initViewerJs(kr.viewerjs);
};

const themeInit = () => {
Expand All @@ -668,7 +671,7 @@ window.copyCode =
});
});

initPpageScrollDown();
initPageScrollDown();
initOffcanvas();
initPerPage();
window.addEventListener("pjax:complete", () => {
Expand Down
6 changes: 4 additions & 2 deletions src/scss/kr-core.scss
Original file line number Diff line number Diff line change
Expand Up @@ -962,14 +962,16 @@ table {
top: 60px;
z-index: 1000;
width: 240px;
height: calc(100% - 60px);
padding: 20px;
overflow-y: auto;
transition: right 0.3s;
-webkit-transition: right 0.3s;
background: rgba(42, 42, 42, 0.9);
right: -240px;

.ul-me {
padding-bottom: 60px;
&.show {
right: 0;
}

ul {
Expand Down

0 comments on commit a1ce002

Please sign in to comment.