博客美化记录
写在前面
众所周知, 一个好的前端/排版是继续做下去的动力(bushi) 。
难得有空,继续来建设一下博客,简单美化一下前端。
自定义 JS/CSS/HTML
引入自定义的 JS/CSS/HTML 是继续下列美化操作的条件,可以通过以下主题配置(楼主使用的是 Fluid 主题,不同主题之间可能存在一些差异)。你可以在配置文件中找到:
# 指定自定义 .js 文件路径,支持列表;路径是相对 source 目录,如 /js/custom.js 对应存放目录 source/js/custom.js# Specify the path of your custom js file, support list. The path is relative to the source directory, such as `/js/custom.js` corresponding to the directory `source/js/custom.js`custom_js:- /js/custom.js
# 指定自定义 .css 文件路径,用法和 custom_js 相同# The usage is the same as custom_jscustom_css:- /css/custom.css自行添加自定义的 JS/CSS/HTML 文件即可。
全局字体
- 首先在
custom.css中引入字体 CDN ;
/* 字体 */@import url('https://chinese-fonts-cdn.deno.dev/packages/maple-mono-cn/dist/MapleMono-CN-Bold/result.css');- 然后修改主题配置文件的字体部分,例如:
# 主题字体配置# Fontfont:font_size: 16pxfont_family: 'Maple Mono CN'font_weight: 400letter_spacing: 0.02emcode_font_size: 85%文章界面背景毛玻璃效果
- 在 source/css 目录下新建样式文件
frostedglassbackground.css,内容如下:
#board { -webkit-backdrop-filter: blur(15px); backdrop-filter: blur(15px);}
#toc { padding: 10px; top: 4rem; background-color: var(--board-bg-color); border-radius: 10px; -webkit-backdrop-filter: blur(15px); backdrop-filter: blur(15px);}- 引入自定义的 CSS 文件,操作略;
- 修改主题配置文件
_config.fluid.yml的以下字段:
# 主面板背景色# Color of main boardboard_color: "#ffffff80"board_color_dark: "#15172280"统一图片宽度
markdown 本身的图片渲染大小就是一大玄学,这里直接采用插入自定义样式文件匹配图片元素,将其固定为页面大小的 70% ,CSS 文件内容如下:
.markdown-body p > img,.markdown-body p > a > img,.markdown-body figure > img,.markdown-body figure > a > img { width: 70%; /* 强制宽度为页面的70% */ height: auto; /* 保持图片比例 */ display: block; margin-left: auto; margin-right: auto; /* 图片居中 */}.page-content img, .post-content img { width: 70%; /* 强制宽度为页面的70% */ height: auto; /* 保持图片比例 */ display: block; margin-left: auto; margin-right: auto; /* 图片居中 */}Mac 样式代码块
在 source/css 目录下新建样式文件 mac.styl ,内容如下:
.highlight background: #011627 border-radius: 5px box-shadow: 0 10px 30px 0 rgba(0, 0, 0, .4) padding-top: 30px
&::before background: #fc625d border-radius: 50% box-shadow: 20px 0 #fdbc40, 40px 0 #35cd4b content: ' ' height: 12px left: 12px margin-top: -20px position: absolute width: 12px然后在配置文件 _config.fluid.yml 中引入该自定义样式文件:
custom_css:- /css/custom.css- /css/mac.css注意,不要写成 /css/mac.styl ,否则会被错误识别为 /css/mac.styl.css 。
折叠框中的方式有一定 BUG,并且会和我的伪代码渲染冲突,如需参考还是尽量先参考上文的方法吧,楼主会在之后有空尽可能提供支持
- 安装 Hexo Shiki Plugin 插件:
# 在博客根目录下执行npm install hexo-shiki-plugin --save- 添加/修改该插件有关配置:
# 在 Hexo 配置文件 config.yml 中# 以下部分一般是初始配置文件中就有的内容syntax_highlighter: # 留空,原本为 highlight.jshighlight: enable: falseprismjs: enable: false
# 添加该插件的配置shiki: theme: one-dark-pro # highlight-theme:one-dark-pro / github-light / github-dark / material-theme-palenight line_number: true # whether to show the line_number beautify: true # whether to add highlight tool true or false highlight_copy: true # copy button highlight_lang: true # show the code language highlight_height_limit: 360 # code-block max height,unit: px is_highlight_shrink: false # true: shrink the code blocks / false: expand the code blocks | none: expand code blocks and hide the button copy: # copy message success: 'Copy Success' error: 'Copy Error' no_support: 'Browser Not Support'- 修改主题配置文件
_config_fluid.yml:
code: copy_btn: false language: enable: false highlight: enable: false上述操作主要都是为了禁用 Hexo 和主题原有的 Highlight 相关内容,避免造成冲突。
- 引入图标文件:
添加 Font Awesome 图标库,这里用的是 360 的 CDN,也可以选择其他的,在 custom.css 中添加即可:
@import url('https://lib.baomitu.com/font-awesome/6.1.2/css/all.min.css');仿知乎链接卡片
- 在 source/css 目录下新建样式文件
linkcard.css,内容如下:
:root { --linkcard-bg-color: #eeefef;}[data-user-color-scheme="dark"] { --linkcard-bg-color: #242a38;}
.LinkCard, .LinkCard:hover { text-decoration: none; border: none !important; color: inherit !important;}.LinkCard { position: relative; display: block; margin: 1em auto; width: 60%; box-sizing: border-box; border-radius: 12px; max-width: 100%; overflow: hidden; color: inherit; text-decoration: none; background: var(--linkcard-bg-color); transition: transform 0.4s ease-in;}.ztext { word-break: break-word; line-height: 1.6; }.LinkCard-content { position: relative; display: flex; align-items: center; justify-content: space-between; padding: 12px; border-radius: inherit; background-color: var(--linkcard-bg-color);}.LinkCard-text { overflow: hidden; }.LinkCard-title { display: -webkit-box; -webkit-line-clamp: 2; line-clamp: 2; overflow: hidden; text-overflow: ellipsis; max-height: calc(16px * 1.25 * 2); font-size: 16px; font-weight: 500; line-height: 1.25; color: inherit;}.LinkCard-meta { display: flex; margin-top: 4px; font-size: 14px; line-height: 20px; color: #999; white-space: nowrap;}.LinkCard-imageCell { margin-left: 8px; border-radius: 6px;}.LinkCard-image { display: block; width: 60px !important; height: auto !important; border-radius: inherit; margin-bottom: 0 !important;}.LinkCard:hover { transform: translate3d(0, -2px, 0); box-shadow: 0 0 16px 0 rgba(34, 135, 250, 0.8);}- 在 source/js 目录下新建自定义 js 文件:
window.onload = function () { var LinkCards = document.getElementsByClassName('LinkCard'); for (var i = 0; i < LinkCards.length; i++) { // 截断链接 var truncateLink = function(url, maxLength) { if (url.length <= maxLength) return url; return url.slice(0, maxLength) + '...'; };
var LinkCard = LinkCards[i]; var link = LinkCard.href; var title = LinkCard.innerText; // 如果没有指定logo地址,则使用默认的loading图片 var logourl = LinkCard.name ? LinkCard.name : '/img/loading/loading1.gif'; var displayLink = truncateLink(link, 32);
LinkCard.innerHTML = `<span class="LinkCard-content"> <span class="LinkCard-text"> <span class="LinkCard-title">${title}</span> <span class="LinkCard-meta"> <span style="display:inline-flex;align-items:center"> <svg class="Zi Zi--InsertLink" fill="currentColor" viewBox="0 0 24 24" width="17" height="17"> <path d="M6.77 17.23c-.905-.904-.94-2.333-.08-3.193l3.059-3.06-1.192-1.19-3.059 3.058c-1.489 1.489-1.427 3.954.138 5.519s4.03 1.627 5.519.138l3.059-3.059-1.192-1.192-3.059 3.06c-.86.86-2.289.824-3.193-.08zm3.016-8.673l1.192 1.192 3.059-3.06c.86-.86 2.289-.824 3.193.08.905.905.94 2.334.08 3.194l-3.059 3.06 1.192 1.19 3.059-3.058c1.489-1.489 1.427-3.954-.138-5.519s-4.03-1.627-5.519-.138L9.786 8.557zm-1.023 6.68c.33.33.863.343 1.177.029l5.34-5.34c.314-.314.3-.846-.03-1.176-.33-.33-.862-.344-1.176-.03l-5.34 5.34c-.314.314-.3.846.03 1.177z" fill-rule="evenodd"></path> </svg> </span> <a href="${link}" title="${link}" style="color:inherit;text-decoration:none;">${displayLink}</a> </span> </span> <span class="LinkCard-imageCell"> <img class="LinkCard-image" alt="logo" src="${logourl}" onerror="this.src='/img/loading/loading1.gif'"> </span> </span>`; // 如果获取指定logo地址失败,则使用默认的loading图片 }}- 在主题配置文件中引入,此处演示略。
- 使用方法:
在文章中直接插入:
<a href="你的链接" name="链接的ico地址" class="LinkCard">你的卡片标题</a>- 使用效果:
这也是这文的部分内容参考的博客,感谢博主提供的思路,如有侵权,请联系我删除!使用致歉!
樱花特效
这里直接用来该网站的 API 接口。并在自定义 js 文件中添加如下内容:
$(document).ready(function() { // 判断是否为首页 if (location.pathname === '/' || location.pathname === '/index.html' || location.pathname === '/index') { // 加载樱花特效 $.getScript("https://api.vvhan.com/api/script/yinghua"); }});我只在首页展示了该特效,所以在前面加了条件判断。
高斯模糊
1. 设置方法
- 首先,引入自定义 CSS 文件:
/* 高斯模糊样式 */.blur-text { filter: blur(5px); transition: filter 0.3s ease;}
.blur-text:hover { filter: none;}- 你可以直接在文章中通过 HTML 标签插入,比如:
<span class="blur-text">这是被模糊的内容</span>- 也可以创建 Hexo 标签插件:
- 在博客根目录创建
scripts文件夹(如果不存在); - 创建一个新文件
scripts/blur.js,内容如下:
- 在博客根目录创建
/** * 模糊文字标签插件 * 使用方法: {% blur 这是被模糊的内容 %} */hexo.extend.tag.register('blur', function(args, content) { var text = args.join(' '); return '<span class="blur-text">' + text + '</span>';}, {ends: false});
/** * 支持多行内容的模糊文字标签插件 * 使用方法: * {% blur_block %} * 这是被模糊的内容 * 可以包含多行 * {% end_blur_block %} */hexo.extend.tag.register('blur_block', function(args, content) { return '<div class="blur-text">' + hexo.render.renderSync({text: content, engine: 'markdown'}) + '</div>';}, {ends: true});2. 配置示例(新站不再动态渲染)
这段内容是正常显示的。
这段内容被模糊了,鼠标悬停或点击可以查看。
或者使用标签插件:
文本黑幕
与高斯模糊的原理是类似的。
1. 使用方法
- 引入自定义 CSS 文件:
/* 文本黑幕样式 */.shady { position: relative; display: inline-block; padding: 0 2px; border-radius: 2px; cursor: pointer;}
.shady::before { content: ""; position: absolute; top: 0; left: 0; width: 100%; height: 100%; background-color: #000; border-radius: 2px; transition: opacity 0.3s ease; z-index: 1;}
.shady:hover::before { opacity: 0;}
.shady > * { position: relative; z-index: 0;}- 你可以直接在文章中通过 HTML 标签插入,比如:
<span class="shady">这是被黑幕遮挡的内容</span>- 也可以创建 Hexo 标签插件:
- 在博客根目录创建
scripts文件夹(如果不存在); - 创建一个新文件
scripts/shady.js,内容如下:
- 在博客根目录创建
/** * 文本黑幕标签插件 * 使用方法: {% shady 这是被黑幕遮挡的内容 %} */hexo.extend.tag.register('shady', function(args, content) { var text = args.join(' '); return '<span class="shady"><span>' + text + '</span></span>';}, {ends: false});
/** * 支持多行内容的文本黑幕标签插件 * 使用方法: * {% shady_block %} * 这是被黑幕遮挡的内容 * 可以包含多行 * {% end_shady_block %} */hexo.extend.tag.register('shady_block', function(args, content) { return '<div class="shady"><div>' + hexo.render.renderSync({text: content, engine: 'markdown'}) + '</div></div>';}, {ends: true});2. 配置示例(新站不再动态渲染)
这是正常显示的文本。
这是一段被黑幕遮挡的内容,鼠标悬停可以查看。
或者使用标签插件:
插入图表
使用 EChart 工具,该工具比较成熟且能绘制的图表多样。
1. 使用方法
- 安装
hexo-tag-echarts插件:
npm install hexo-tag-echarts --save- 在需要插入图表的文章开头添加以下 js 内容:
// 通过jsDelivr的CDN引入echarts<script src="https://cdn.jsdelivr.net/npm/echarts@4.8.0/dist/echarts.min.js"></script>// 使用GL里的各种组件时需要添加,否则可不需要<script src="https://cdn.jsdelivr.net/npm/echarts-gl@1.1.1/dist/echarts-gl.min.js"></script>注:这里不通过 js 文件直接引入是因为用到的地方比较少,没必要作为必要加载。(博客里塞的自定义小垃圾已经够多了,加上没做优化,尽可能还是少加载一些)
- 插入图表内容:
{% echarts 400 '85%' %}...{% endecharts %}具体格式说明参考 Echart官网 ;
2. 配置示例(新站不再动态渲染)
这里以折线图为例:
option = { title: { text: 'Stacked Area Chart' }, tooltip: { trigger: 'axis', axisPointer: { type: 'cross', label: { backgroundColor: '#6a7985' } } }, legend: { data: ['Email', 'Union Ads', 'Video Ads', 'Direct', 'Search Engine'] }, toolbox: { feature: { saveAsImage: {} } }, grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true }, xAxis: [ { type: 'category', boundaryGap: false, data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] } ], yAxis: [ { type: 'value' } ], series: [ { name: 'Email', type: 'line', stack: 'Total', areaStyle: {}, emphasis: { focus: 'series' }, data: [120, 132, 101, 134, 90, 230, 210] }, { name: 'Union Ads', type: 'line', stack: 'Total', areaStyle: {}, emphasis: { focus: 'series' }, data: [220, 182, 191, 234, 290, 330, 310] }, { name: 'Video Ads', type: 'line', stack: 'Total', areaStyle: {}, emphasis: { focus: 'series' }, data: [150, 232, 201, 154, 190, 330, 410] }, { name: 'Direct', type: 'line', stack: 'Total', areaStyle: {}, emphasis: { focus: 'series' }, data: [320, 332, 301, 334, 390, 330, 320] }, { name: 'Search Engine', type: 'line', stack: 'Total', label: { show: true, position: 'top' }, areaStyle: {}, emphasis: { focus: 'series' }, data: [820, 932, 901, 934, 1290, 1330, 1320] } ]};首页文章滑入动画
- 在 source/js 目录下新建自定义 JS 文件
scrollanimation.js,内容如下:
const cards = document.querySelectorAll('.index-card')if (cards.length) { document.querySelector('.row').setAttribute('style', 'overflow: hidden;') const coefficient = document.documentElement.clientWidth > 768 ? .5 : .3 const origin = document.documentElement.clientHeight - cards[0].getBoundingClientRect().height * coefficient
function throttle(fn, wait) { let timer = null; return function () { const context = this; const args = arguments; if (!timer) { timer = setTimeout(function () { fn.apply(context, args); timer = null; }, wait) } } }
function handle() { cards.forEach(card => { card.setAttribute('style', `--state: ${(card.getBoundingClientRect().top - origin) < 0 ? 1 : 0};`) }) }
document.addEventListener("scroll", throttle(handle, 100));}- 在 source/css 目录下新建自定义样式文件
scrollanimation.css,内容如下:
.index-card { transition: all 0.5s; transform: scale(calc(1.25 - 0.25 * var(--state))); opacity: var(--state); margin-bottom: 2rem;}
.index-img img { margin: 20px 0;}- 在配置文件中引入自定义 css 和 js 文件,操作略;
网页元素悬停放大特效
- 在 source/css 目录下新建自定义样式文件
hover.css,内容如下:
/* 首页文章封面 */.index-img { /* 动画时间 */ transition: .4s;}.index-card:hover .index-img { /* 放大倍数 */ transform: scale(1.05);}
/* 头像 */.about-avatar img { transition: .4s;}.about-avatar:hover img { transform: scale(1.05);}
/* 标签云 */.tagcloud a { transition: .4s;}.tagcloud a:hover { transform: scale(1.05);}你可以自由补充你希望悬停放大的网页元素。
- 引入自定义样式文件,操作略;
标签页文字跟随焦点切换
原理是添加一个对页面可见性事件 visibilitychange 的监听。
- 在 source/js 目录下新建自定义 JS 文件
visibilitychange.js,内容如下:
jQuery(document).ready(function() { var b, c, a = document.title; var welcomeTimer = null; function d() { if (document[b]) { document.title = " 你切到别的标签页了 "; if (welcomeTimer) { clearTimeout(welcomeTimer); welcomeTimer = null; } } else { document.title = " 你在当前标签页 "; welcomeTimer = setTimeout(function() { document.title = a; }, 2000); } }
if (typeof document.hidden !== "undefined") { b = "hidden"; c = "visibilitychange"; } else if (typeof document.mozHidden !== "undefined") { b = "mozHidden"; c = "mozvisibilitychange"; } else if (typeof document.webkitHidden !== "undefined") { b = "webkitHidden"; c = "webkitvisibilitychange"; }
if ((typeof document.addEventListener !== "undefined" || typeof document[b] !== "undefined") && c) { document.addEventListener(c, d, false); }});- 引入自定义 JS 文件,操作略;
后记
持续更新中。没什么技术含量,主要是一些小构思。
文章分享
如果这篇文章对你有帮助,欢迎分享给更多人!



