给安和鱼主题的博客挂上红灯笼

今天我们来给安知鱼主题的 Hexo 博客添红灯笼,临近春节的时候,很多小伙伴的博客都开始悬挂红灯笼,喜气洋洋,今天我们来看实现教程。

1.添加信息字段

我们需要对博客根目录下面的 _config.yml 进行字段添加,打开这个文件之后,找一个合适的位置添加以下代码

1
2
3
denglong:
enable: true # true 开启 false 关闭
text: "新年快乐" # 配置灯笼的四个字

主要设置目的,就是为了方便开启红灯笼,毕竟春节和其他节庆日时间有限,需要祝贺的内容也不相同。

2.添加并引用 CSS

完成了第一步后我们需要为红灯笼进行样式定义。

在博客主题的 /source/ 目录中的 css 文件夹中,创建一个名为 coustom.css 的文件。

在新建的 custom.css 文件中添加如下代码:

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* 新年灯笼🏮 */
.denglong {
pointer-events: none;
}

.deng-box1 {
position: fixed;
z-index: 9999;
top: -30px;
left: -1px;
}

.deng-box2 {
position: fixed;
z-index: 9999;
top: -25px;
left: 184px;
}

.deng-box3 {
position: fixed;
z-index: 9999;
top: -28px;
right: 11px;
}

.deng-box4 {
position: fixed;
z-index: 9999;
top: -26px;
right: 183px;
}

.deng {
position: relative;
width: 120px;
height: 90px;
margin: 50px;
background: rgba(216, 0, 15, 0.8);
border-radius: 50%;
transform-origin: 50% -100px;
animation: swing 3s infinite ease-in-out;
box-shadow: -5px 5px 50px 4px rgba(250, 108, 0, 1);
}

.deng:before {
position: absolute;
top: -7px;
left: 29px;
height: 12px;
width: 60px;
content: " ";
display: block;
z-index: 999;
border-radius: 5px 5px 0 0;
border: solid 1px #dc8f03;
background: linear-gradient(to right, #dc8f03, #ffa500, #dc8f03, #ffa500, #dc8f03);
}

.deng:after {
position: absolute;
bottom: -7px;
left: 10px;
height: 12px;
width: 60px;
content: " ";
display: block;
margin-left: 20px;
border-radius: 0 0 5px 5px;
border: solid 1px #dc8f03;
background: linear-gradient(to right, #dc8f03, #ffa500, #dc8f03, #ffa500, #dc8f03);
}

.deng-a {
width: 100px;
height: 90px;
background: rgba(216, 0, 15, 0.1);
margin: 12px 8px 8px 10px;
border-radius: 50%;
border: 2px solid #dc8f03;
}

.deng-b {
width: 45px;
height: 90px;
background: rgba(216, 0, 15, 0.1);
margin: -2px 8px 8px 26px;
border-radius: 50%;
border: 2px solid #dc8f03;
}

.xian {
position: absolute;
top: -50px;
left: 60px;
width: 2px;
height: 50px;
background: #dc8f03;
}

.shui-a {
position: relative;
width: 5px;
height: 20px;
margin: -5px 0 0 59px;
animation: swing 4s infinite ease-in-out;
transform-origin: 50% -45px;
background: #ffa500;
border-radius: 0 0 5px 5px;
}

.shui-b {
position: absolute;
top: 14px;
left: -2px;
width: 10px;
height: 10px;
background: #dc8f03;
border-radius: 50%;
}

.shui-c {
position: absolute;
top: 18px;
left: -2px;
width: 10px;
height: 35px;
background: #ffa500;
border-radius: 0 0 0 5px;
}

.deng-t {
font-family: "华文行楷", Arial, Lucida Grande, Tahoma, sans-serif;
font-size: 3.2rem;
color: #dc8f03;
font-weight: bold;
line-height: 85px;
text-align: center;
}

@keyframes swing {
0% {
transform: rotate(-10deg);
}
50% {
transform: rotate(10deg);
}
100% {
transform: rotate(-10deg);
}
}

/* 适配暗色模式 */
[data-theme="dark"] .deng {
background: rgba(216, 0, 15, 0.8);
}

[data-theme="dark"] .deng-a,
[data-theme="dark"] .deng-b {
background: rgba(216, 0, 15, 0.1);
}

@media screen and (max-width: 768px) {
.deng-box1,
.deng-box2,
.deng-box3,
.deng-box4 {
display: none !important;
}
}

做好上面的文件之后,在安知鱼主题的配置文件中搜索 inject ,就会找到如下代码

1
2
3
4
5
6
7
8
9
10
11
# Inject
# Insert the code to head (before '</head>' tag) and the bottom (before '</body>' tag)
# 插入代码到头部 </head> 之前 和 底部 </body> 之前
inject:
head:
# 自定义css
# - <link rel="stylesheet" href="/css/custom.css" media="defer" onload="this.media='all'">

bottom:
# 自定义js
# - <script src="/js/xxx"></script>

head 处进行自定义 css 的引入工作:

1
- <link rel="stylesheet" href="/css/custom.css">

修改完成之后的代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
# Inject
# Insert the code to head (before '</head>' tag) and the bottom (before '</body>' tag)
# 插入代码到头部 </head> 之前 和 底部 </body> 之前
inject:
head:
# 自定义css
# - <link rel="stylesheet" href="/css/custom.css" media="defer" onload="this.media='all'">
- <link rel="stylesheet" href="/css/custom.css">

bottom:
# 自定义js
# - <script src="/js/xxx"></script>
- <script src="/js/countdown.js"></script>

3.修改 layout.pug文件

紧接着我们需要修改模板引擎 /themes/anzhiyu/layout/includes/layout.pug 文件,这里特别要注意缩进,Pug 的缩进至关重要,它直接决定了代码能否正确运行以及最终生成的 HTML 结构。可是使用下面的代码进行完整替换

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
- var htmlClassHideAside = theme.aside.enable && theme.aside.hide ? 'hide-aside' : ''
- page.aside = is_archive() ? theme.aside.display.archive: is_category() ? theme.aside.display.category : is_tag() ? theme.aside.display.tag : page.aside
- var hideAside = !theme.aside.enable || page.aside === false ? 'hide-aside' : ''
- var pageType = is_post() ? 'post' : 'page'

doctype html
html(lang=config.language data-theme=theme.display_mode class=htmlClassHideAside)
head
include ./head.pug
if theme.denglong.enable
- const denglongText = theme.denglong.text || "新年快乐"; // 如果未配置,默认显示“新年快乐”
.denglong
each char, index in denglongText
div(class=`deng-box${index + 1}`)
.deng
.xian
.deng-a
.deng-b
.deng-t= char
.shui.shui-a
.shui-c
.shui-b


body(data-type="anzhiyu")
#web_bg
#an_music_bg
if theme.preloader.enable
!=partial('includes/loading/index', {}, {cache: true})
if (theme.mourn.enable && is_home_first_page())
include ./mourn.pug
if page.type !== '404'
#body-wrap(class=pageType)
include ./header/index.pug
main#blog-container
if (is_home())
include ./bbTimeList.pug
if is_current("/")
include ./top/top.pug
if page.top_single
- let background = page.top_single_background
- let tip = page.top_single_tip
- let subTitle = page.top_single_subtitle
- let btn_link = page.top_single_btn_link
- let btn_text = page.top_single_btn_text
#single_top
.author-content.author-content-item.single(style=`${background ? `background: url(${background}) top / cover no-repeat;` : ""}`)
.card-content
.author-content-item-tips=subTitle
span.author-content-item-title=page.title
.content-bottom
.tips=tip
.banner-button-group
a.banner-button(onclick=`pjax.loadUrl("${url_for(btn_link ? btn_link : '/about')}")`)
i.anzhiyufont.anzhiyu-icon-arrow-circle-right(style='font-size: 1.5rem')
span.banner-button-text=btn_text ? btn_text : "关于我"

#content-inner.layout(class=hideAside)
if body
div!= body
else
block content
if theme.aside.enable && page.aside !== false
include widget/index.pug

- var footerBg = theme.footer_bg
if (footerBg)
if (footerBg === true)
- var footer_bg = bg_img
else
- var footer_bg = theme.footer_bg.indexOf('/') !== -1 ? `background-image: url('${url_for(footerBg)}')` : `background: ${footerBg}`
else
- var footer_bg = ''

footer#footer(style=footer_bg)
!=partial('includes/footer', {}, {cache: true})

if (theme.agreementPopup && theme.agreementPopup.enable && is_home_first_page())
- let agreementPopupUrl = theme.agreementPopup.url
script(defer).
var hasShownPopup = sessionStorage.getItem('sessionNegotiatePopupShown');

if (!hasShownPopup) {
setTimeout(() => {
anzhiyuPopupManager && anzhiyuPopupManager.enqueuePopup('协议提醒助手', '查看本站为你的个人隐私做出的努力', '#{agreementPopupUrl}', 4000);
sessionStorage.setItem('sessionNegotiatePopupShown', 'true');
}, 1000);
}

else
include ./404.pug

!=partial('includes/sidebar', {}, {cache: true})

if theme.shortcutKey.enable
!=partial('includes/shortcutKey', {}, {cache: true})
include ./rightside.pug

if (theme.nav_music.enable || theme.nav_music.console_widescreen_music)
include ./music.pug
!=partial('includes/third-party/search/index', {}, {cache: true})
!=partial('includes/anzhiyu/rightmenu', {}, {cache:true})
include ./additional-js.pug

//- 弹窗通知
!=partial('includes/popup/index', {}, {cache: true})

上面的代码只需要复制粘贴替换掉 layout.pug 文件即可,实际修改的代码并不复杂,在 layout.pug 文件中找到

1
2
3
4
doctype html
html(lang=config.language data-theme=theme.display_mode class=htmlClassHideAside)
head
include ./head.pug

然后在这段代码下面添加

1
2
3
4
5
6
7
8
9
10
11
12
13
if theme.denglong.enable
- const denglongText = theme.denglong.text || "新年快乐"; // 如果未配置,默认显示“新年快乐”
.denglong
each char, index in denglongText
div(class=`deng-box${index + 1}`)
.deng
.xian
.deng-a
.deng-b
.deng-t= char
.shui.shui-a
.shui-c
.shui-b

如果出现问题,可是看一看代码的缩进问题,毕竟 Pug 的缩进至关重要,最简单的方法是替换,前提是没有修改过这个文件。

4 结束语

最后 Hexo 三连(hexo cl && hexo g && hexo s)就可以看到文章开始页面顶部的效果。