butterfly主题魔改10:分类页面魔改
参考 https://akilar.top/posts/a9131002/
效果图

01 安装插件
安装命令
1
| npm install hexo-butterfly-categories-card --save
|
02 新建样式文件
在博客根目录themes\butterfly\source\css\
下新建categorybar.css
文件,添加 https://npm.elemecdn.com/hexo-butterfly-categories-card@1.0.0/lib/categorybar.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
| #categoryBar { width: 100% !important; } ul.categoryBar-list { margin: 5px 5px 0 5px !important; padding: 0 !important; } li.categoryBar-list-item { font-weight: bold; display: inline-block; height: 180px !important; margin: 5px 0.5% 0 0.5% !important; background-image: linear-gradient(rgba(0,0,0,0.4) 25%, rgba(16,16,16,0) 100%); border-radius: 10px; padding: 25px 0 25px 25px !important; box-shadow: rgba(50,50,50,0.3) 50px 50px 50px 50px inset; overflow: hidden; background-size: 100% !important; background-position: center !important; } li.categoryBar-list-item:hover { background-size: 110% !important; box-shadow: inset 500px 50px 50px 50px rgba(50,50,50,0.6); } li.categoryBar-list-item:hover span.categoryBar-list-descr { transition: all 0.5s; transform: translate(-100%, 0); } a.categoryBar-list-link { color: #fff !important; font-size: 20px !important; } a.categoryBar-list-link::before { content: '|' !important; color: #fff !important; font-size: 20px !important; } a.categoryBar-list-link:after { content: ''; position: relative; width: 0; bottom: 0; display: block; height: 3px; border-radius: 3px; background-color: #fff; } a.categoryBar-list-link:hover:after { width: 90%; left: 1%; transition: all 0.5s; } span.categoryBar-list-count { display: block !important; color: #fff !important; font-size: 20px !important; } span.categoryBar-list-count::before { content: '\f02d' !important; padding-right: 15px !important; display: inline-block; font-weight: 600; font-style: normal; font-variant: normal; font-family: 'Font Awesome 6 Free'; text-rendering: auto; -webkit-font-smoothing: antialiased; } span.categoryBar-list-descr { padding: 5px; display: block !important; color: #fff !important; font-size: 20px !important; position: relative; right: -100%; } @media screen and (max-width: 650px) { li.categoryBar-list-item { width: 48% !important; height: 150px !important; margin: 5px 1% 0 1% !important; } }
|
03 修改插件js样式文件
打开博客根目录下node_modules\hexo-butterfly-categories-card\index.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 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
| 'use strict'
const pluginname = 'butterfly_categories_card'
const pug = require('pug') const path = require('path') const urlFor = require('hexo-util').url_for.bind(hexo) const util = require('hexo-util')
hexo.extend.filter.register('after_generate', function () {
const config = hexo.config.categoryBar || hexo.theme.config.categoryBar if (!(config && config.enable)) return var categories_list= hexo.locals.get('categories').data; var categories_message= config.message; var new_categories_list = []; for (var i = 0; i < categories_list.length; i++) { var a = categories_list[i]; var b = categories_message[i]; new_categories_list[i] = Object.assign({}, a, { path: a.path }, b); } const data = { pjaxenable: hexo.theme.config.pjax.enable, enable_page: config.enable_page ? config.enable_page : "/", layout_type: config.layout.type, layout_name: config.layout.name, layout_index: config.layout.index ? config.layout.index : 0, categories_list: new_categories_list, column: config.column ? config.column : odd, row: config.row ? config.row : 2, custom_css: config.custom_css ? urlFor(config.custom_css) : "https://cdn.jsdelivr.net/npm/hexo-butterfly-categories-card/lib/categorybar.css" } const temple_html_text = config.temple_html ? config.temple_html : pug.renderFile(path.join(__dirname, './lib/html.pug'),data);
const css_text = `<link rel="stylesheet" href="${data.custom_css}">` var get_layout if (data.layout_type === 'class') { get_layout = `document.getElementsByClassName('${data.layout_name}')[${data.layout_index}]` } else if (data.layout_type === 'id') { get_layout = `document.getElementById('${data.layout_name}')` } else { get_layout = `document.getElementById('${data.layout_name}')` }
var user_info_js = `<script data-pjax> function ${pluginname}_injector_config(){ // 检查容器是否存在 var parent_div_git = ${get_layout}; // 如果容器不存在,则动态创建 if (!parent_div_git) { console.warn('${pluginname}: 挂载容器不存在,正在动态创建...'); // 创建新容器(默认插入到页面主体顶部) parent_div_git = document.createElement('div'); parent_div_git.id = '${data.layout_name}'; // 赋予配置的ID document.querySelector('#page').prepend(parent_div_git); // 插入到 #content-inner 内 } var item_html = '${temple_html_text}'; console.log('已挂载 ${pluginname}'); parent_div_git.insertAdjacentHTML("afterbegin",item_html) } // 路径匹配逻辑(使用 startsWith) if (location.pathname.startsWith('${data.enable_page}') || '${data.enable_page}' === 'all') { ${pluginname}_injector_config(); } </script>` hexo.extend.injector.register('body_end', user_info_js, "default"); hexo.extend.injector.register('head_end', css_text, "default"); }, hexo.extend.helper.register('priority', function(){ const pre_priority = hexo.config.categoryBar.priority || hexo.theme.config.categoryBar.priority const priority = pre_priority ? pre_priority : 10 return priority }) )
|
04 修改插件html.pug文件
打开博客根目录下node_modules\hexo-butterfly-categories-card\lib\html.pug
文件,全部替换成以下代码:
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
| - var prow = 190 * row + 'px' - var mrow = 160 * row + 'px'
if (column === 'odd') style. li.categoryBar-list-item{width:32.3%;}.categoryBar-list{max-height: #{prow};overflow:auto;}.categoryBar-list::-webkit-scrollbar{width:0!important}@media screen and (max-width: 650px){.categoryBar-list{max-height: #{mrow};}} else if (column === 'even') style. li.categoryBar-list-item{width:24%;}.categoryBar-list{max-height: #{prow};overflow:auto;}.categoryBar-list::-webkit-scrollbar{width:0!important}@media screen and (max-width: 650px){.categoryBar-list{max-height: #{mrow};}} .recent-post-item(style='height:auto;width:100%;padding:0px;') #categoryBar ul.categoryBar-list if pjaxenable each cl in categories_list li.categoryBar-list-item(style=`background:url(` + cl.cover + `);`) //- 强制修正路径:移除重复的 categories 前缀 - var cleanPath = cl.path.replace(/^\/?categories\//, '') a.categoryBar-list-link( onclick=`pjax.loadUrl("/categories/${cleanPath}");` href='javascript:void(0);' )= cl.name span.categoryBar-list-count= cl.length span.categoryBar-list-descr= cl.descr else each cl in categories_list li.categoryBar-list-item(style=`background:url(` + cl.cover + `);`) //- 直接输出修正后的路径 - var cleanPath = cl.path.replace(/^\/?categories\//, '') a.categoryBar-list-link(href=`/categories/${cleanPath}`)= cl.name span.categoryBar-list-count= cl.length span.categoryBar-list-descr= cl.descr
|
05 修改配置文件
在博客根目录下_config.butterfly.yml
文件添加以下代码:
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
|
categoryBar: enable: true priority: 5 enable_page: /categories/ layout: type: id name: recent-posts index: 0 column: odd row: 5 message: - descr: 1 cover: /img/11.png - descr: 2 cover: /img/12.png - descr: 3 cover: /img/13.png - descr: 4 cover: /img/14.png - descr: 5 cover: /img/15.png - descr: 6 cover: /img/16.png - descr: 7 cover: /img/17.png - descr: 8 cover: /img/18.png custom_css: /css/categorybar.css
|