Mobile wallpaper 1Mobile wallpaper 2Mobile wallpaper 3Mobile wallpaper 4Mobile wallpaper 5Mobile wallpaper 6
919 字
5 分钟
打造亮点:Solitude 主题文章融入波浪效果

一. 修改 header.pug#

首先,我们需要定位到 header.pug 文件,其路径如下:

themes/
└── solitude/
└── layout/
└── includes/
└── header.pug

接下来,在该文件中找到合适的位置(通常是在文章元信息之后),在最后添加以下代码:

section.main-hero-waves-area.waves-area
svg.waves-svg(xmlns='http://www.w3.org/2000/svg', xlink='http://www.w3.org/1999/xlink', viewBox='0 24 150 28', preserveAspectRatio='none', shape-rendering='auto')
defs
path#gentle-wave(d='M -160 44 c 30 0 58 -18 88 -18 s 58 18 88 18 s 58 -18 88 -18 s 58 18 88 18 v 44 h -352 Z')
g.parallax
use(href='#gentle-wave', x='48', y='0')
use(href='#gentle-wave', x='48', y='3')
use(href='#gentle-wave', x='48', y='5')
use(href='#gentle-wave', x='48', y='7')
NOTE

需要注意的是,Pug 是一种依赖缩进来表示代码块结构的模板语言(Template Language),因此在复制粘贴时务必保证缩进正确

二. 注册样式文件#

接下来,我们需要为波浪效果添加对应的 CSS 样式。请将以下 CSS 代码保存为你新建的 css 文件,例如:博客根目录/source/css/baiyi.css

建议将自定义的资源文件放到博客根目录的 source 文件夹下。这样做的好处是,无论你更换什么主题,这些资源文件都能被正确引用,不会受到主题的影响。而且 Hexo 会默认处理 source 文件夹下的内容,不需要额外的配置。

因此,你可以把 baiyi.css 文件放到 source/css 文件夹中,然后在主题的配置文件里使用 <link rel="stylesheet" href="/css/baiyi.css"> 来引用它。

以下是完整的 CSS 样式代码:

/* 波浪css */
.main-hero-waves-area {
width: 100%;
position: absolute;
left: 0;
bottom: -11px;
z-index: 5;
}
.waves-area .waves-svg {
width: 100%;
height: 5rem;
}
/* Animation */
.parallax > use {
animation: move-forever 25s cubic-bezier(0.55, 0.5, 0.45, 0.5) infinite;
}
.parallax > use:nth-child(1) {
animation-delay: -2s;
animation-duration: 7s;
fill: #f7f9febd;
}
.parallax > use:nth-child(2) {
animation-delay: -3s;
animation-duration: 10s;
fill: #f7f9fe82;
}
.parallax > use:nth-child(3) {
animation-delay: -4s;
animation-duration: 13s;
fill: #f7f9fe36;
}
.parallax > use:nth-child(4) {
animation-delay: -5s;
animation-duration: 20s;
fill: #f7f9fe;
}
/* 黑色模式背景 */
[data-theme="dark"] .parallax > use:nth-child(1) {
animation-delay: -2s;
animation-duration: 7s;
fill: #18171dc8;
}
[data-theme="dark"] .parallax > use:nth-child(2) {
animation-delay: -3s;
animation-duration: 10s;
fill: #18171d80;
}
[data-theme="dark"] .parallax > use:nth-child(3) {
animation-delay: -4s;
animation-duration: 13s;
fill: #18171d3e;
}
[data-theme="dark"] .parallax > use:nth-child(4) {
animation-delay: -5s;
animation-duration: 20s;
fill: #18171d;
}
@keyframes move-forever {
0% {
transform: translate3d(-90px, 0, 0);
}
100% {
transform: translate3d(85px, 0, 0);
}
}
/*Shrinking for mobile*/
@media (max-width: 768px) {
.waves-area .waves-svg {
height: 40px;
min-height: 40px;
}
}

三. 额外可选配置#

如果你希望波浪效果仅在电脑端显示,而在手机端不显示,可以通过媒体查询(Media Query)来实现。只需将上述 CSS 代码包裹在以下媒体查询语句中:

/* 仅在屏幕宽度大于 768px 时应用以下样式 */
@media (min-width: 769px) {
/* 波浪css */
.main-hero-waves-area {
width: 100%;
position: absolute;
left: 0;
bottom: -11px;
z-index: 5;
}
.waves-area .waves-svg {
width: 100%;
height: 5rem;
}
/* Animation */
.parallax > use {
animation: move-forever 25s cubic-bezier(0.55, 0.5, 0.45, 0.5) infinite;
}
.parallax > use:nth-child(1) {
animation-delay: -2s;
animation-duration: 7s;
fill: #f7f9febd;
}
.parallax > use:nth-child(2) {
animation-delay: -3s;
animation-duration: 10s;
fill: #f7f9fe82;
}
.parallax > use:nth-child(3) {
animation-delay: -4s;
animation-duration: 13s;
fill: #f7f9fe36;
}
.parallax > use:nth-child(4) {
animation-delay: -5s;
animation-duration: 20s;
fill: #f7f9fe;
}
/* 黑色模式背景 */
[data-theme="dark"] .parallax > use:nth-child(1) {
animation-delay: -2s;
animation-duration: 7s;
fill: #18171dc8;
}
[data-theme="dark"] .parallax > use:nth-child(2) {
animation-delay: -3s;
animation-duration: 10s;
fill: #18171d80;
}
[data-theme="dark"] .parallax > use:nth-child(3) {
animation-delay: -4s;
animation-duration: 13s;
fill: #18171d3e;
}
[data-theme="dark"] .parallax > use:nth-child(4) {
animation-delay: -5s;
animation-duration: 20s;
fill: #18171d;
}
@keyframes move-forever {
0% {
transform: translate3d(-90px, 0, 0);
}
100% {
transform: translate3d(85px, 0, 0);
}
}
/*Shrinking for mobile*/
@media (max-width: 768px) {
.waves-area .waves-svg {
height: 40px;
min-height: 40px;
}
}
}

四. 引入样式文件#

最后一步,我们需要在主题的 _config.yml 文件中引入刚才创建的 CSS 文件。具体操作如下:

_config.yml 中找到 extends 配置项,并在 head 下添加自定义 CSS 的链接:

# --------------------------- start ---------------------------
# Extend
# 扩展
extends:
# Insert in head
# 插入到 head
head:
# 自定义css
- <link rel="stylesheet" href="/css/baiyi.css">
# Insert in body
# 插入到 body
body:
# - <script src="https://cdn.bootcdn.net/ajax/libs/pace/1.2.4/pace.min.js"></script>
# --------------------------- end ---------------------------

完成以上步骤后,重新部署你的 Hexo 博客,即可看到文章顶部的动态波浪效果。

打造亮点:Solitude 主题文章融入波浪效果
https://jizhiben.xin/posts/打造视觉亮点solitude-主题文章融入波浪效果/
作者
林间
发布于
2025-12-17
许可协议
MIT

部分信息可能已经过时

封面
Sample Song
Sample Artist
封面
Sample Song
Sample Artist
0:00 / 0:00