首页的大标题和小标题还有logo显示霓虹灯闪烁

黑夜霓虹灯

此教程会有两处地方有霓虹灯效果:一个是大标题和个人信息的动态霓虹灯,默认周期为 1200ms;另外的是菜单栏的小字有夜光效果,为你的博客增添几分赛博朋克风~

1. 首先在自定义的样式文件

[BlogRoot]\source\css\custom.css 中引入以下代码,这部分代码是菜单栏文字有夜光效果的,变量部分 var(--theme-color) 可以换为自己喜欢的颜色,例如紫色 rgb(179, 71, 241)

1
2
3
4
5
6
7
8
9
10
/* 夜间模式菜单栏发光字 */
[data-theme="dark"] #nav .site-page,
[data-theme="dark"] #nav .menus_items .menus_item .menus_item_child li a {
text-shadow: 0 0 2px var(--theme-color) !important;
}

/* 手机端适配 */
[data-theme="dark"] #sidebar #sidebar-menus .menus_items .site-page {
text-shadow: 0 0 2px var(--theme-color) !important;
}

2. 新建文件

[BlogRoot]\source\js\light.js 并写入以下代码,本质就是计时器,大家可以根据自己的喜好调节闪烁周期,默认为 1200ms

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// 霓虹灯效果
// 颜色数组
var arr = ["#39c5bb", "#f14747", "#f1a247", "#f1ee47", "#b347f1", "#1edbff", "#ed709b", "#5636ed"];
// 颜色索引
var idx = 0;

// 切换颜色
function changeColor() {
// 仅夜间模式才启用
if (document.getElementsByTagName('html')[0].getAttribute('data-theme') == 'dark') {
if (document.getElementById("site-name"))
document.getElementById("site-name").style.textShadow = arr[idx] + " 0 0 15px";
if (document.getElementById("site-title"))
document.getElementById("site-title").style.textShadow = arr[idx] + " 0 0 15px";
if (document.getElementById("site-subtitle"))
document.getElementById("site-subtitle").style.textShadow = arr[idx] + " 0 0 10px";
if (document.getElementById("post-info"))
document.getElementById("post-info").style.textShadow = arr[idx] + " 0 0 5px";
try {
document.getElementsByClassName("author-info__name")[0].style.textShadow = arr[idx] + " 0 0 12px";
document.getElementsByClassName("author-info__description")[0].style.textShadow = arr[idx] + " 0 0 12px";
} catch {

}
idx++;
if (idx == 8) {
idx = 0;
}
} else {
// 白天模式恢复默认
if (document.getElementById("site-name"))
document.getElementById("site-name").style.textShadow = "#1e1e1ee0 1px 1px 1px";
if (document.getElementById("site-title"))
document.getElementById("site-title").style.textShadow = "#1e1e1ee0 1px 1px 1px";
if (document.getElementById("site-subtitle"))
document.getElementById("site-subtitle").style.textShadow = "#1e1e1ee0 1px 1px 1px";
if (document.getElementById("post-info"))
document.getElementById("post-info").style.textShadow = "#1e1e1ee0 1px 1px 1px";
try {
document.getElementsByClassName("author-info__name")[0].style.textShadow = "";
document.getElementsByClassName("author-info__description")[0].style.textShadow = "";
} catch {

}
}
}

// 开启计时器
window.onload = setInterval(changeColor, 1200);

3. 引入文件

在主题配置文件_config.butterfly.yml 引入以上两个文件,要注意的是,js 文件这里必须为 defer,不能为 ansyc,保证脚本会延迟到整个页面都解析完后再执行,此时才有对应的元素进行操作:

1
2
3
4
5
inject:
head:
- <link rel="stylesheet" href="/css/custom.css" media="defer" onload="this.media='all'">
bottom:
- <script defer src="/js/light.js"></script> # 霓虹灯(必须defer否则有时候会不生效)

4. 重启项目即可看到效果

1
hexo cl; hexo s

来源于猕猴桃小哥哥

博客魔改教程总结 (一) | Fomalhaut🥝

效果看顶部点击我到顶部