ppoffice / hexo-theme-icarus

A simple, delicate, and modern theme for the static site generator Hexo.
https://ppoffice.github.io/hexo-theme-icarus/
MIT License
6.28k stars 1.54k forks source link

引入 Pjax 以加速网页切换速度 #1277

Open PJ-568 opened 4 months ago

PJ-568 commented 4 months ago

引入 Pjax 以加速网页切换速度

引入 Pjax 后一些主题相关的 JS 需在每次 Ajax 时重载,鄙人才疏学浅,无法定位相关 JS 。 本主题有计划添加 Pjax 支持吗?

目前发现的问题:

此方案后的网站:blog.PJ568.sbs

引入方式

方案选择

设置并初始化 Pjax

// 初始化并设置哪些需要改变
var pjax = new Pjax({
    selectors: [
        "head title",
        ".columns",
        ".navbar-start",
        ".navbar-end",
        ".searchbox",
        "#back-to-top",
        "[data-pjax]",
        ".pjax-reload"
    ],
});

// 包含一些 Pjax 加载后需要重新执行的函数
function pjax_reload() {
    // 需要重新执行的函数写在这
}

// 监听 Pjax 完成后,重新加载上面的函数
document.addEventListener("pjax:complete", function () {
    pjax_reload();
});

脚本中添加了 .pjax-reload[data-pjax] 。若切换页面时,某些元素需重新加载,可添加 .pjax-reload 到该元素,脚本可添加 [data-pjax] 属性。

利用 Hexo 注入器功能引入 Pjax 和初始化脚本:

const js = hexo.extend.helper.get('js').bind(hexo);

hexo.extend.injector.register('head_end', () => {
  return js('//lib.baomitu.com/pjax/0.2.8/pjax.min.js');
});

hexo.extend.injector.register('head_end', () => {
  return js('/js/SetPjax.js');
});

相关 issues 和 Discussions :