Skip to content

Commit

Permalink
【修复】加载动画
Browse files Browse the repository at this point in the history
  • Loading branch information
PJ-568 committed May 22, 2024
1 parent 3370913 commit c4ec571
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 50 deletions.
40 changes: 0 additions & 40 deletions assets/load_ani.js

This file was deleted.

59 changes: 50 additions & 9 deletions assets/override.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(function() {
(function () {
// giscus 评论系统
//// 若当前 mdbook 主题为 Light 或 Rust ,则将 giscus 主题设置为 light
const getCurrentTheme = function () {
Expand Down Expand Up @@ -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
Expand All @@ -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",
Expand All @@ -118,7 +138,7 @@
]
})
}
catch(e){
catch (e) {
console.log('PJAX 出错:' + e);
}
}
Expand All @@ -128,6 +148,7 @@
function initialize() {
initPjax(); //// 初始化 PJAX
initTranslate(); //// 初始化页面翻译
initAni(); //// 初始化加载动画
SetupGiscus(getCurrentLanguage(), getCurrentTheme()); //// 初始化评论系统
}

Expand All @@ -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();
})
})();
2 changes: 1 addition & 1 deletion book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand Down

0 comments on commit c4ec571

Please sign in to comment.