From bd6a4c84cb76ec18d1ce171b6eff6144dcae6803 Mon Sep 17 00:00:00 2001 From: liuzhihang Date: Tue, 25 Oct 2022 22:38:45 +0800 Subject: [PATCH] =?UTF-8?q?js=20=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- templates/assets/js/main.js | 114 +++++++++++++++--------------------- 1 file changed, 48 insertions(+), 66 deletions(-) diff --git a/templates/assets/js/main.js b/templates/assets/js/main.js index b499d5ab..a0fb061b 100644 --- a/templates/assets/js/main.js +++ b/templates/assets/js/main.js @@ -39,69 +39,49 @@ function _arrayLikeToArray(t, e) { } document.addEventListener("DOMContentLoaded", function () { - - let blogNameWidth, menusWidth, searchWidth, $nav; - - // 调整菜单 - function adjustMenu(init) { - - if (init) { - blogNameWidth = document.getElementById("site-name").offsetWidth; - const $menusEle = document.querySelectorAll("#menus .menus_item"); - menusWidth = 0; - $menusEle.length && $menusEle.forEach((i) => { - menusWidth += i.offsetWidth; - }); - const $searchEle = document.querySelector("#search-button"); - searchWidth = $searchEle ? $searchEle.offsetWidth : 0; - $nav = document.getElementById("nav"); - } - let hideMenuIndex; - if (window.innerWidth <= 768) { - hideMenuIndex = true; - } else { - hideMenuIndex = blogNameWidth + menusWidth + searchWidth > $nav.offsetWidth - 120; - } - - if (hideMenuIndex) { - $nav.classList.add("hide-menu"); - } else { - $nav.classList.remove("hide-menu"); - } + function L(t) { + 0 < arguments.length && void 0 !== t && t && (i = o && o.offsetWidth, c = a && a.offsetWidth, s = r && r.offsetWidth); + var e = document.getElementById("nav"), n = window.innerWidth < 768 || i + c + s > e.offsetWidth - 120; + n ? e.classList.add("hide-menu") : e.classList.remove("hide-menu") } - // function l(t) { - // function e(t) { - // t.each(function (t, e) { - // var n = $(e), o = n.attr("data-lazy-src") || n.attr("src"), i = o + "_1600w", a = n.attr("alt") || ""; - // -1 != o.indexOf("!blogimg") ? n.wrap('')) : n.wrap('')) - // }), $().fancybox({ - // selector: "[data-fancybox]", - // loop: !0, - // transitionEffect: "slide", - // protect: !0, - // buttons: ["slideShow", "fullScreen", "thumbs", "close"], - // hash: !1 - // }) - // } - // - // void 0 === $.fancybox ? ($("head").append('')), $.getScript("".concat(GLOBAL_CONFIG.source.fancybox.js), function () { - // e($(t)) - // })) : e($(t)) - // } - // - // function w() { - // var n = "fancybox" === GLOBAL_CONFIG.lightbox ? document.querySelectorAll("#article-container :not(a):not(.gallery-group) > img, #article-container > img,.bber-content-img > img") : [], - // o = 0 < n.length, i = document.querySelectorAll("#article-container .justified-gallery"), a = 0 < i.length; - // (a || o) && btf.isJqueryLoad(function () { - // var t, e; - // a && (t = $(i), (e = t.find("img")).unwrap(), e.length && e.each(function (t, e) { - // $(e).attr("data-lazy-src") && $(e).attr("src", $(e).attr("data-lazy-src")), $(e).wrap("
") - // }), d ? btf.initJustifiedGallery(t) : ($("head").append('')), $.getScript("".concat(GLOBAL_CONFIG.source.justifiedGallery.js), function () { - // btf.initJustifiedGallery(t) - // }), d = !0)), o && l(n) - // }) - // } + var o = document.getElementById("site-name"), i = o && o.offsetWidth, + a = document.querySelector("#menus .menus_items"), c = a && a.offsetWidth, + r = document.querySelector("#search-button"), s = r && r.offsetWidth; + + function l(t) { + function e(t) { + t.each(function (t, e) { + var n = $(e), o = n.attr("data-lazy-src") || n.attr("src"), i = o + "_1600w", a = n.attr("alt") || ""; + -1 !== o.indexOf("!blogimg") ? n.wrap('')) : n.wrap('')) + }); + $().fancybox({ + selector: "[data-fancybox]", + loop: !0, + transitionEffect: "slide", + protect: !0, + buttons: ["slideShow", "fullScreen", "thumbs", "close"], + hash: !1 + }) + } + + void 0 === $.fancybox ? ($("head").append('')), $.getScript("".concat(GLOBAL_CONFIG.source.fancybox.js), function () { + e($(t)) + })) : e($(t)) + } + + function w() { + var n = "fancybox" === GLOBAL_CONFIG.lightbox ? document.querySelectorAll("#article-container :not(a):not(.gallery-group) > img, #article-container > img,.bber-content-img > img") : [], + o = 0 < n.length, i = document.querySelectorAll("#article-container .justified-gallery"), a = 0 < i.length; + (a || o) && btf.isJqueryLoad(function () { + var t, e; + a && (t = $(i), (e = t.find("img")).unwrap(), e.length && e.each(function (t, e) { + $(e).attr("data-lazy-src") && $(e).attr("src", $(e).attr("data-lazy-src")), $(e).wrap("
") + }), d ? btf.initJustifiedGallery(t) : ($("head").append('')), $.getScript("".concat(GLOBAL_CONFIG.source.justifiedGallery.js), function () { + btf.initJustifiedGallery(t) + }), d = !0)), o && l(n) + }) + } function S() { var t = document.getElementById("card-toc"), r = t.getElementsByClassName("toc-content")[0], @@ -111,7 +91,8 @@ document.addEventListener("DOMContentLoaded", function () { var t = window.scrollY || document.documentElement.scrollTop; e(t), i(t) }, 100)() - }, window.addEventListener("scroll", tocScrollFn); + }; + window.addEventListener("scroll", tocScrollFn); var e = function (t) { var e = c.clientHeight, n = document.documentElement.clientHeight, o = (t - c.offsetTop) / (n < e ? e - n : document.documentElement.scrollHeight - n), @@ -124,7 +105,8 @@ document.addEventListener("DOMContentLoaded", function () { }; document.getElementById("mobile-toc-button").addEventListener("click", function () { ("0" === window.getComputedStyle(t).getPropertyValue("opacity") ? n : o)() - }), r.addEventListener("click", function (t) { + }); + r.addEventListener("click", function (t) { t.preventDefault(); var e = t.target.classList.contains("toc-link") ? t.target : t.target.parentElement; btf.scrollToDest(btf.getEleTop(document.getElementById(decodeURI(e.getAttribute("href")).replace("#", ""))), 300), window.innerWidth < 900 && o() @@ -189,7 +171,7 @@ document.addEventListener("DOMContentLoaded", function () { f.style.overflow = "", f.style.paddingRight = "", btf.fadeOut(u, .5), d.classList.remove("open") } - adjustMenu(), document.getElementById("nav").classList.add("show"), GLOBAL_CONFIG_SITE.isPost ? (GLOBAL_CONFIG_SITE.isToc && S(), void 0 !== GLOBAL_CONFIG.noticeOutdate && (r = GLOBAL_CONFIG.noticeOutdate, (s = btf.diffDate(GLOBAL_CONFIG_SITE.postUpdate)) >= r.limitDay && ((a = document.createElement("div")).className = "post-outdate-notice", a.textContent = r.messagePrev + " " + s + " " + r.messageNext, c = document.getElementById("article-container"), "top" === r.position ? c.insertBefore(a, c.firstChild) : c.appendChild(a))), GLOBAL_CONFIG.relativeDate.post && I(document.querySelectorAll("#post-meta time"))) : (GLOBAL_CONFIG.relativeDate.homepage && I(document.querySelectorAll("#recent-posts time")), !GLOBAL_CONFIG.runtime || (i = document.getElementById("runtimeshow")) && (o = i.getAttribute("data-publishDate"), i.innerText = btf.diffDate(o) + " " + GLOBAL_CONFIG.runtime), (n = document.getElementById("last-push-date")) && (e = n.getAttribute("data-lastPushDate"), n.innerText = btf.diffDate(e, !0)), (t = document.querySelectorAll("#aside-cat-list .card-category-list-item.parent i")).length && t.forEach(function (t) { + L(), document.getElementById("nav").classList.add("show"), GLOBAL_CONFIG_SITE.isPost ? (GLOBAL_CONFIG_SITE.isToc && S(), void 0 !== GLOBAL_CONFIG.noticeOutdate && (r = GLOBAL_CONFIG.noticeOutdate, (s = btf.diffDate(GLOBAL_CONFIG_SITE.postUpdate)) >= r.limitDay && ((a = document.createElement("div")).className = "post-outdate-notice", a.textContent = r.messagePrev + " " + s + " " + r.messageNext, c = document.getElementById("article-container"), "top" === r.position ? c.insertBefore(a, c.firstChild) : c.appendChild(a))), GLOBAL_CONFIG.relativeDate.post && I(document.querySelectorAll("#post-meta time"))) : (GLOBAL_CONFIG.relativeDate.homepage && I(document.querySelectorAll("#recent-posts time")), !GLOBAL_CONFIG.runtime || (i = document.getElementById("runtimeshow")) && (o = i.getAttribute("data-publishDate"), i.innerText = btf.diffDate(o) + " " + GLOBAL_CONFIG.runtime), (n = document.getElementById("last-push-date")) && (e = n.getAttribute("data-lastPushDate"), n.innerText = btf.diffDate(e, !0)), (t = document.querySelectorAll("#aside-cat-list .card-category-list-item.parent i")).length && t.forEach(function (t) { t.addEventListener("click", function (t) { t.preventDefault(); this.classList.toggle("expand"); @@ -233,9 +215,9 @@ document.addEventListener("DOMContentLoaded", function () { refreshFn(); - window.addEventListener("resize", adjustMenu); + window.addEventListener("resize", L); window.addEventListener("orientationchange", function () { - setTimeout(adjustMenu(!0), 100) + setTimeout(L(!0), 100) }); document.querySelectorAll("#sidebar-menus .expand").forEach(function (t) {