效果图

本文会详细介绍其修改过程。

bj.png

主题配置

修改 Butterfly 的配置文件 _config.butterfly.yml
编辑 index_imgindex_top_img_heightbackgroundfooter_bgmask.header 选项。
设置网站背景,将主页顶部图和页脚背景改为透明,调整主页顶部图高度,移除顶部图的黑色半透遮罩。
(需要将示例地址替换为自己的图片地址。)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# The banner image of home page
index_img: transparent

# The height of top_img, eg: 300px/300em/300rem (主頁top_img高度)
index_top_img_height: 400px

# The formal of image: url(本地或者网络/xxx.jpg)
background: url(/img/background.jpg)

# Footer Background
footer_bg: transparent

# Add mask to header or footer (为 header 或 footer 添加黑色半透遮罩)
mask:
header: false

引入相关样式

  • 新建一个文件,位于 source/css/modify.styl,并增加以下内容。
  • (此处只是举例,也可以使用已有的样式文件。)
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
  @import 'nib'

// 顶部图
#page-header
background: transparent !important

&.post-bg, &.not-home-page
height: 280px !important
#post-info
bottom: 40px !important
text-align: center
#page-site-info
top: 140px !important

@media screen and (max-width: 768px)
&.not-home-page
height: 200px !important
#post-info
bottom: 10px !important
#page-site-info
top: 100px !important

.top-img
height: 250px
margin: -50px -40px 50px
border-top-left-radius: inherit
border-top-right-radius: inherit
background-position: center center
background-size: cover
transition: all 0.3s

@media screen and (max-width: 768px)
height: 230px
margin: -36px -14px 36px

[data-theme='dark'] &
filter: brightness(0.8)

// 页脚
#footer:before
background-color: alpha(#FFF, .5)

[data-theme='dark'] &
background-color: alpha(#000, .5)

#footer-wrap, #footer-wrap a
color: #111
transition: unset

[data-theme='dark'] &
color: var(--light-grey)

修改 Butterfly 的配置文件 _config.butterfly.yml,在 inject.head 选项引入样式

1
2
3
inject:
head:
- <link rel="stylesheet" href="/css/modify.css">

Hexo 会将 Stylus 渲染成 CSS 文件,所以此处应为 modify.css

增加插件脚本

因为使用了 cheerio 来解析 HTML,所以需要先安装此依赖。

1
npm install cheerio
  • 在hexo-blog/source/新建一个文件,位于 scripts/modify.js,并增加以下内容。
  • (此处只是举例,也可以使用已有的插件脚本文件。)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
'use strict';
const { filter } = hexo.extend;
const cheerio = require('cheerio');

/**
* 在页面插入新顶部图
* @param {cheerio.Root} $ Root
*/
function insertTopImg($) {
const header = $('#page-header');
if (header.length === 0) return;
const background = header.css('background-image');
if (!background) return;
$('#post, #page, #archive, #tag, #category').prepend(`<div class="top-img" style="background-image: ${background};"></div>`);
}

// 修改 HTML
filter.register('after_render:html', (str, data) => {
const $ = cheerio.load(str, {
decodeEntities: false
});
insertTopImg($);
return $.html();
});

大功告成

👏 恭喜你完成了修改,可以使用以下命令进行预览。

1
hexo cl; hexo s