From c4ec57139a77570574305dd1af8fa8ce8a3f5bd5 Mon Sep 17 00:00:00 2001 From: PJ568 Date: Wed, 22 May 2024 09:15:07 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E5=8A=A8=E7=94=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/load_ani.js | 40 ------------------------------- assets/override.js | 59 +++++++++++++++++++++++++++++++++++++++------- book.toml | 2 +- 3 files changed, 51 insertions(+), 50 deletions(-) delete mode 100644 assets/load_ani.js diff --git a/assets/load_ani.js b/assets/load_ani.js deleted file mode 100644 index 89b6c69..0000000 --- a/assets/load_ani.js +++ /dev/null @@ -1,40 +0,0 @@ -var loadingBar = document.querySelector(".loading-bar"); -var progress = document.querySelector(".loading-bar .progress"); -var timer = null; - -// Pjax 开始时执行的函数 -document.addEventListener("pjax:send", function () { - // 进度条默认已经加载 20% - var loadingBarWidth = 20; - // 进度条的最大增加宽度 - var MAX_LOADING_WIDTH = 95; - - // 显示进度条 - loadingBar.classList.add("loading"); - // 初始化进度条的宽度 - progress.style.width = loadingBarWidth + "%"; - - clearInterval(timer); - timer = setInterval(function () { - // 进度条的增加速度(可以改为一个随机值,显得更加真实) - loadingBarWidth += 3; - - // 当进度条到达 95% 后停止增加 - if (loadingBarWidth > MAX_LOADING_WIDTH) { - loadingBarWidth = MAX_LOADING_WIDTH; - } - - progress.style.width = loadingBarWidth + "%"; - }, 500); -}); - -// Pjax 完成之后执行的函数 -document.addEventListener("pjax:complete", function () { - clearInterval(timer); - progress.style.width = "100%"; - loadingBar.classList.remove("loading"); - - setTimeout(function () { - progress.style.width = 0; - }, 400); -}); diff --git a/assets/override.js b/assets/override.js index 3a782bb..fe105a3 100644 --- a/assets/override.js +++ b/assets/override.js @@ -1,4 +1,4 @@ -(function() { +(function () { // giscus 评论系统 //// 若当前 mdbook 主题为 Light 或 Rust ,则将 giscus 主题设置为 light const getCurrentTheme = function () { @@ -72,14 +72,14 @@ // translate-js 翻译系统 function initTranslate() { - try{ + try { translate.service.use('client.edge'); translate.listener.start(); translate.language.setLocal('chinese_simplified'); translate.setAutoDiscriminateLocalLanguage(); translate.language.setUrlParamControl(); translate.ignore.class.push('notTranslate'); - translate.nomenclature.append('chinese_simplified','english',` + translate.nomenclature.append('chinese_simplified', 'english', ` 哈姆语言之书=Hamud Book 哈姆语=the language of Hamud 哈姆德=the Hamud @@ -93,18 +93,38 @@ `); translate.execute(); } - catch(e){ + catch (e) { console.log('翻译系统出错:' + e); } } + var loadingBar = document.querySelector(".loading-bar"); + var progress = document.querySelector(".loading-bar .progress"); + var timer = null; + + function initAni() { + loadingBar = document.querySelector(".loading-bar"); + progress = document.querySelector(".loading-bar .progress"); + } + + function endLoad() { + clearInterval(timer); + progress.style.width = "100%"; + loadingBar.classList.remove("loading"); + + setTimeout(function () { + progress.style.width = 0; + }, 400); + } + + // PJAX 集成 let pjax; //// 初始化 PJAX - function initPjax(){ - try{ - const Pjax = window.Pjax || function() {}; + function initPjax() { + try { + const Pjax = window.Pjax || function () { }; pjax = new Pjax({ selectors: [ "head title", @@ -118,7 +138,7 @@ ] }) } - catch(e){ + catch (e) { console.log('PJAX 出错:' + e); } } @@ -128,6 +148,7 @@ function initialize() { initPjax(); //// 初始化 PJAX initTranslate(); //// 初始化页面翻译 + initAni(); //// 初始化加载动画 SetupGiscus(getCurrentLanguage(), getCurrentTheme()); //// 初始化评论系统 } @@ -136,11 +157,31 @@ //// 网页加载完毕后触发 window.addEventListener('DOMContentLoaded', () => initialize()); //// 翻译执行完成后触发 - translate.listener.renderTaskFinish = function() { + translate.listener.renderTaskFinish = function () { SetupGiscus(getCurrentLanguage(), getCurrentTheme()); } + //// Pjax 开始时执行的函数 + document.addEventListener("pjax:send", function () { + var loadingBarWidth = 20; + var MAX_LOADING_WIDTH = 95; + + loadingBar.classList.add("loading"); + progress.style.width = loadingBarWidth + "%"; + + clearInterval(timer); + timer = setInterval(function () { + loadingBarWidth += 3; + + if (loadingBarWidth > MAX_LOADING_WIDTH) { + loadingBarWidth = MAX_LOADING_WIDTH; + } + + progress.style.width = loadingBarWidth + "%"; + }, 500); + }); //// 监听 Pjax 完成后,重新加载上面的函数 document.addEventListener("pjax:complete", function () { SetupGiscus(getCurrentLanguage(), getCurrentTheme()); + endLoad(); }) })(); \ No newline at end of file diff --git a/book.toml b/book.toml index 07c5b56..5000f20 100644 --- a/book.toml +++ b/book.toml @@ -14,7 +14,7 @@ site-url = "/zh-Hans-CN/" # additional-css = ["assets/loading.css", "assets/title_fix.css", "assets/translate.css"] # additional-js = ["assets/loading.js", "assets/translate_lib.js", "assets/giscus.js", "assets/SetPjax.js", "assets/load_ani.js"] additional-css = ["assets/override.css"] -additional-js = ["assets/override.js", "assets/load_ani.js"] +additional-js = ["assets/override.js"] git-repository-url = "https://github.com/Hamud-Lang/Hamud_Book/tree/zh-Hans-CN" edit-url-template = "https://github.com/Hamud-Lang/Hamud_Book/edit/zh-Hans-CN/{path}"