Skip to content

Commit c40c8ab

Browse files
committed
【新增】优化 CSS 加载并添加图片懒加载
- 添加 loadCSS 函数,用于异步加载 CSS 文件 - 在 initialize 函数中加载字体 CSS - 为所有 img 标签添加 loading="lazy" 属性,实现图片懒加载 - 移除 head 中的字体 CSS 链接,改为通过 JavaScript 动态加载
1 parent 8f33f72 commit c40c8ab

1 file changed

Lines changed: 40 additions & 3 deletions

File tree

index.html

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!doctype html>
22

33
<html>
4-
<markdown-html version="1.17.9" author="PJ568" repo="https://github.com/PJ-568/markdown.html"
4+
<markdown-html version="1.18.0" author="PJ568" repo="https://github.com/PJ-568/markdown.html"
55
license="CC BY-SA 4.0 International"></markdown-html>
66

77
<head>
@@ -326,6 +326,41 @@
326326
}
327327
}
328328

329+
// 加载 CSS
330+
function loadCSS(url) {
331+
return new Promise((resolve, reject) => {
332+
//// 检查是否已加载
333+
const existingLinks = document.querySelectorAll(`link[href="${url}"]`);
334+
if (existingLinks.length > 0) {
335+
return resolve(url);
336+
}
337+
338+
//// 创建新的 link 元素
339+
const link = document.createElement('link');
340+
link.rel = 'stylesheet';
341+
link.type = 'text/css';
342+
link.href = url;
343+
344+
//// 成功加载处理
345+
link.onload = () => {
346+
link.onload = null;
347+
link.onerror = null;
348+
resolve(url);
349+
};
350+
351+
//// 加载失败处理
352+
link.onerror = () => {
353+
link.onerror = null;
354+
link.onload = null;
355+
document.head.removeChild(link);
356+
reject(new Error(`无法加载CSS文件: ${url}`));
357+
};
358+
359+
//// 添加到文档头部
360+
document.head.appendChild(link);
361+
});
362+
}
363+
329364
// 等待动画结束
330365
function waitForAnimationsEnd(parentElement, selector, animationNames) {
331366
return new Promise(resolve => {
@@ -494,6 +529,7 @@
494529
const images = linkDoc.querySelectorAll('img');
495530
images.forEach(img => {
496531
const src = img.getAttribute('src');
532+
img.setAttribute('loading', 'lazy');
497533
if (src && !isExternalLink(src) && isRelativePath(src)) {
498534
var absolutePath = pValue ? getDirectory(mdPath) + src : getPath() + src;
499535
absolutePath = new URL(absolutePath, 'http://example.com').pathname;
@@ -590,6 +626,7 @@
590626

591627
// 初始化
592628
function initialize() {
629+
startLoad(); //// 开始加载动画
593630
initDarkmode(); //// 初始化深色模式
594631
initHomeBtn(); //// 初始化返回首页按钮
595632
initAboutBtn(); //// 初始化关于按钮
@@ -598,6 +635,8 @@
598635
initPJAX(); //// 初始化 PJAX
599636
initCustomPJAXResponse(); //// 初始化自定义 PJAX 响应
600637
initCustomPJAXEventListener(); //// 初始化自定义 PJAX 事件监听器
638+
loadCSS('https://chinese-fonts-cdn.deno.dev/packages/maple-mono-cn/dist/MapleMono-CN-Regular/result.css'); //// 加载字体 CSS
639+
endLoad(); //// 结束加载动画
601640
}
602641

603642
// 触发器
@@ -1017,8 +1056,6 @@
10171056
</style>
10181057
<link href="https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-y/highlight.js/11.4.0/styles/default.min.css"
10191058
type="text/css" rel="stylesheet" />
1020-
<link rel="stylesheet"
1021-
href="https://chinese-fonts-cdn.deno.dev/packages/maple-mono-cn/dist/MapleMono-CN-Regular/result.css" />
10221059
</head>
10231060

10241061
<body>

0 commit comments

Comments
 (0)