Hexo引入自定义HTML,JS,CSS
使用Injector注入器(推荐)
在博客根目录scripts
文件夹下创建任意名称的js文件:
1 2 3
| hexo.extend.injector.register('body_end', `<script src="/js/custom.js"></script>`, 'default');
|
参数说明:
body_end
:注入位置(可替换为head_end
等)- ``:注入内容
default
:作用范围(全局)
官方Injector文档参考
使用css/js引入的方法
在/source/assets/js/
文件夹下新建js文件,写入相应代码内容。配置好代码文件后打开主题配置文件_config.yml找到”custom_js”和”custom_css”下面进行引入。
1 2 3 4 5
| custom_js: - /assets/js/custom.js - https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js custom_css: - /assets/css/custom.css
|
常用特效资源
JS特效
建议添加defer属性,defer可以实现延迟执行脚本,下载则不会被阻塞。可防止阻塞HTM 的DOM构建
<script defer src="app.js"></script>
1 2 3 4 5 6
| 樱花特效:https://cdn.jsdelivr.net/gh/Ukenn2112/UkennWeb@3.0/index/web.js 鼠标移动小星星特效:https://static.kevinchu.top/blog/assets/js/stars.js 礼花打字特效:https://static.kevinchu.top/blog/assets/js/typing-effect.js 小雪花飘落效果:https://cdn.jsdelivr.net/gh/EmoryHuang/BlogBeautify@1.1/snowflake.min.js 鼠标点击出字(defer):https://cdn.jsdelivr.net/gh/EmoryHuang/BlogBeautify@1.1/containsWord.min.js 鼠标点击爱心(defer):https://cdn.jsdelivr.net/gh/EmoryHuang/BlogBeautify@1.1/love.min.js
|
更多特效参考:Hexo-Fluid博客主题美化 - Chen’s Blog (chen320.github.io)
鼠标点击特效(烟花特效):纯Javascript实现鼠标点击特效(烟花特效)_js鼠标点击效果-CSDN博客
浏览器标题恶搞
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| var OriginTitle = document.title; var titleTime; document.addEventListener('visibilitychange', function () { if (document.hidden) { $('[rel="icon"]').attr('href', "/img/favicon.png"); document.title = '╭(°A°`)╮ 页面崩溃啦 ~'; clearTimeout(titleTime); } else { $('[rel="icon"]').attr('href', "/img/favicon.png"); document.title = '(ฅ>ω<*ฅ) 噫又好啦 ~' + OriginTitle; titleTime = setTimeout(function () { document.title = OriginTitle; }, 2000); } });
|
CSS渲染
1 2 3 4 5 6 7
| 鼠标:https://cdn.jsdelivr.net/gh/EmoryHuang/BlogBeautify@1.1/shubiao.css 滚动条颜色:https://cdn.jsdelivr.net/gh/EmoryHuang/BlogBeautify@1.1/scroll.css 头部打字机颜色效果渐变:https://cdn.jsdelivr.net/gh/EmoryHuang/BlogBeautify@1.1/gradient.css // 增加下面的css,针对文章标题 background-size: auto !important; }
|
标题颜色,首行缩进,代码框表格框折叠:
hexo博客fluid主题美化 - Gilgamesh’s Blog (gilgamesh-lzq.github.io)
设置表格最大高度
1 2 3 4 5 6 7 8
| figure table { padding: 0; word-break: initial; overflow-x: auto; max-height: 30em; overflow-y: auto !important; }
|
黑暗模式作用域(适配黑暗模式):
1 2 3 4 5 6 7
| [data-user-color-scheme='dark'] body{ background-image: none; } [data-user-color-scheme='dark'] #board{ background-image: none; }
|
自定义博客字体
Web字体在线生成:Online @font-face generator — Transfonter
CSS免费在线字体格式转换器 CSS @font-face 生成器_font-face生成器-CSDN博客
两个实现了自定义字体的博客:
Hexo Fluid主题 添加自定义字体 - Largesse’s blog
Hexo’s Fluid 主题私人定制(持续更新) - Eren の 宇宙船 (erenship.com)
压缩静态资源
博客中有大量 HTML、CSS、JS 文件,这些文件为了阅读方便会加入许多回车和空行,但在页面解析时其实会浪费部分时间,此外如果有许多插图,也会拖慢网页加载,并占据 GitHub 仓库的存储空间。
目前有关插件有 gulp
、hexo-neat
、hexo-all-minifier
。推荐采用集成度比较高的 hexo-all-minifier
来实现,由于在安装依赖包过程报错,本站最终采用了 hexo-neat
。
1 2
| $ npm install hexo-all-minifier --save $ npm install hexo-neat --save
|
在配置文件 _config.yml
中增加如下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
neat_enable: true
neat_html: enable: true exclude:
neat_css: enable: true exclude: - '**/*.min.css'
neat_js: enable: true mangle: true output: compress: exclude: - '**/*.min.js' - '**/jquery.fancybox.pack.js' - '**/index.js'
|
Fluid主题美化参考