From 9d47fe017bf1a4772ece099f9e6c2d74c431d91c Mon Sep 17 00:00:00 2001 From: liuzhihang Date: Sat, 29 Oct 2022 20:17:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20js=20=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=85=A8=E5=B1=80=E9=85=8D=E7=BD=AE=20gloabl=5Fconfig?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- templates/assets/js/main.js | 179 +++++++++++------------ templates/assets/js/utils.js | 16 +- templates/category.html | 2 +- templates/index.html | 2 +- templates/modules/layouts/layout.html | 29 +++- templates/modules/widgets/aside/toc.html | 1 + templates/page.html | 2 +- templates/post.html | 2 +- templates/tag.html | 2 +- 9 files changed, 119 insertions(+), 116 deletions(-) diff --git a/templates/assets/js/main.js b/templates/assets/js/main.js index 9e3f7c8a..5b3ca51d 100644 --- a/templates/assets/js/main.js +++ b/templates/assets/js/main.js @@ -46,7 +46,6 @@ document.addEventListener("DOMContentLoaded", function () { let initTop = 0; let $header = document.getElementById("page-header"); - let $cookies = document.getElementById("cookies-window"); window.scrollCollect = btf.throttle(function () { @@ -66,7 +65,6 @@ document.addEventListener("DOMContentLoaded", function () { } } $header.classList.add("nav-fixed") - $cookies.classList.add("cw-hide") } else { if (0 === currentTop) { $header.classList.remove("nav-fixed", "nav-visible") @@ -80,30 +78,23 @@ document.addEventListener("DOMContentLoaded", function () { // 滚动处理 const scrollFnToDo = function () { let $cardTocLayout = document.getElementById("card-toc"); - let $cardToc = $cardTocLayout.getElementsByClassName("toc-content")[0]; - let $tocLink = $cardToc.querySelectorAll(".toc-link"); + // let $cardToc = $cardTocLayout.getElementsByClassName("toc-content")[0]; + // let $tocLink = $cardToc.querySelectorAll(".toc-link"); let $article = document.getElementById("article-container"); - - const scrollPercent = function (currentTop) { - const docHeight = $article.clientHeight; - const winHeight = document.documentElement.clientHeight; - const headerHeight = $article.offsetTop; - const contentMath = (docHeight > winHeight) ? (docHeight - winHeight) : (document.documentElement.scrollHeight - winHeight) - const scrollPercent = (currentTop - headerHeight) / (contentMath); - const scrollPercentRounded = Math.round(100 * scrollPercent); - const percentage = 100 < scrollPercentRounded ? 100 : scrollPercentRounded <= 0 ? 0 : scrollPercentRounded; - $cardToc.setAttribute("progress-percentage", percentage); - } - - document.getElementById("mobile-toc-button") - .addEventListener("click", function () { - if ("0" === window.getComputedStyle($cardTocLayout).getPropertyValue("opacity")) { - window.mobileToc.open(); - } else { - window.mobileToc.close(); - } - }); + // let scrollPercent; + // if (GLOBAL_CONFIG.htmlType === 'post') { + // scrollPercent = function (currentTop) { + // const docHeight = $article.clientHeight; + // const winHeight = document.documentElement.clientHeight; + // const headerHeight = $article.offsetTop; + // const contentMath = (docHeight > winHeight) ? (docHeight - winHeight) : (document.documentElement.scrollHeight - winHeight) + // const scrollPercent = (currentTop - headerHeight) / (contentMath); + // const scrollPercentRounded = Math.round(100 * scrollPercent); + // const percentage = 100 < scrollPercentRounded ? 100 : scrollPercentRounded <= 0 ? 0 : scrollPercentRounded; + // $cardToc.setAttribute("progress-percentage", percentage); + // }; + // } window.mobileToc = { open: () => { @@ -119,79 +110,79 @@ document.addEventListener("DOMContentLoaded", function () { } // toc 元素点击 - $cardToc.addEventListener("click", e => { - e.preventDefault(); - const target = e.target.classList.contains("toc-link") ? e.target : e.target.parentElement; - - btf.scrollToDest( - btf.getEleTop( - document.getElementById( - decodeURI(target.getAttribute("href")).replace("#", "") - ) - ), - 300 - ); - if (window.innerWidth < 900) { - window.mobileToc.close(); - } - }); + // $cardToc.addEventListener("click", e => { + // e.preventDefault(); + // const target = e.target.classList.contains("toc-link") ? e.target : e.target.parentElement; + // + // btf.scrollToDest( + // btf.getEleTop( + // document.getElementById( + // decodeURI(target.getAttribute("href")).replace("#", "") + // ) + // ), + // 300 + // ); + // if (window.innerWidth < 900) { + // window.mobileToc.close(); + // } + // }); // find head position & add active class - const list = $article.querySelectorAll("h1,h2,h3,h4,h5,h6"); - let detectItem = ""; - const findHeadPosition = function (top) { + // const list = $article.querySelectorAll("h1,h2,h3,h4,h5,h6"); + // let detectItem = ""; + // const findHeadPosition = function (top) { + // + // if (0 === $tocLink.length || 0 === top) { + // return false + // } + // + // let currentId = ""; + // let currentIndex = ""; + // + // list.forEach(function (ele, index) { + // if (top > btf.getEleTop(ele) - 80) { + // currentId = "#" + encodeURI(ele.getAttribute("id")); + // currentIndex = index + // } + // + // }) + // if (detectItem === currentIndex) { + // return + // } + // + // detectItem = currentIndex + // + // $cardToc.querySelectorAll(".active").forEach(i => { + // i.classList.remove("active") + // }) + // + // if (currentId === "") { + // return + // } + // + // const currentActive = $tocLink[currentIndex] + // currentActive.classList.add("active") + // + // setTimeout(() => { + // autoScrollToc(currentActive) + // }, 0) + // + // if (isExpand) return + // let parent = currentActive.parentNode + // + // for (; !parent.matches(".toc"); parent = parent.parentNode) { + // if (parent.matches("li")) parent.classList.add("active") + // } + // } - if (0 === $tocLink.length || 0 === top) { - return false - } - - let currentId = ""; - let currentIndex = ""; - - list.forEach(function (ele, index) { - if (top > btf.getEleTop(ele) - 80) { - currentId = "#" + encodeURI(ele.getAttribute("id")); - currentIndex = index - } - - }) - if (detectItem === currentIndex) { - return - } - - detectItem = currentIndex - - $cardToc.querySelectorAll(".active").forEach(i => { - i.classList.remove("active") - }) - - if (currentId === "") { - return - } - - const currentActive = $tocLink[currentIndex] - currentActive.classList.add("active") - - setTimeout(() => { - autoScrollToc(currentActive) - }, 0) - - if (isExpand) return - let parent = currentActive.parentNode - - for (; !parent.matches(".toc"); parent = parent.parentNode) { - if (parent.matches("li")) parent.classList.add("active") - } - } - - window.tocScrollFn = function () { - return btf.throttle(function () { - let currentTop = window.scrollY || document.documentElement.scrollTop; - scrollPercent(currentTop) - findHeadPosition(currentTop); - }, 100)(); - }; - window.addEventListener("scroll", tocScrollFn); + // window.tocScrollFn = function () { + // return btf.throttle(function () { + // let currentTop = window.scrollY || document.documentElement.scrollTop; + // // scrollPercent(currentTop) + // findHeadPosition(currentTop); + // }, 100)(); + // }; + // window.addEventListener("scroll", tocScrollFn); }; const tabsFn = { diff --git a/templates/assets/js/utils.js b/templates/assets/js/utils.js index 45d213fb..a4ab33a3 100644 --- a/templates/assets/js/utils.js +++ b/templates/assets/js/utils.js @@ -167,10 +167,6 @@ const btf = { } }, - isJqueryLoad: function (e) { - "undefined" == typeof jQuery ? getScript(GLOBAL_CONFIG.source.jQuery).then(e) : e() - }, - isHidden: ele => ele.offsetHeight === 0 && ele.offsetWidth === 0, getEleTop: ele => { @@ -183,16 +179,6 @@ const btf = { } return actualTop - }, - - updateAnchor: (anchor) => { - if (anchor !== window.location.hash) { - if (!anchor) anchor = location.pathname - const title = GLOBAL_CONFIG_SITE.title - window.history.replaceState({ - url: location.href, - title: title - }, title, anchor) - } } + }; \ No newline at end of file diff --git a/templates/category.html b/templates/category.html index 6c6acab3..f3306d76 100644 --- a/templates/category.html +++ b/templates/category.html @@ -1,5 +1,5 @@ - + diff --git a/templates/index.html b/templates/index.html index 3a461e5d..c1a804f6 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,5 +1,5 @@ - + diff --git a/templates/modules/layouts/layout.html b/templates/modules/layouts/layout.html index fcf04008..fb324c55 100644 --- a/templates/modules/layouts/layout.html +++ b/templates/modules/layouts/layout.html @@ -1,8 +1,33 @@ - + - + + + + + + diff --git a/templates/modules/widgets/aside/toc.html b/templates/modules/widgets/aside/toc.html index 727589ca..1c919854 100644 --- a/templates/modules/widgets/aside/toc.html +++ b/templates/modules/widgets/aside/toc.html @@ -7,6 +7,7 @@
文章目录 +
diff --git a/templates/page.html b/templates/page.html index 9d299909..b6052543 100644 --- a/templates/page.html +++ b/templates/page.html @@ -1,5 +1,5 @@ - + diff --git a/templates/post.html b/templates/post.html index 0931cf14..e908cb88 100644 --- a/templates/post.html +++ b/templates/post.html @@ -1,5 +1,5 @@ - +
diff --git a/templates/tag.html b/templates/tag.html index 1a8ecf15..9bf43efc 100644 --- a/templates/tag.html +++ b/templates/tag.html @@ -1,5 +1,5 @@ - +