{"title":"自定义分类条","slug":"33249733","url":"https://blog.biss.click/posts/33249733/","date":"2025-08-14T16:25:49.000Z","updated":null,"summary":"这篇文章介绍了如何在博客系统中自定义分类栏的设计和实现过程。首先,通过Pug模板语言创建了一个名为categoryBar.pug的文件,用于定义分类栏的结构和样式。接着,在Stylus样式文件category-bar.styl中详细定义了分类栏的样式,包括内边距、背景颜色、边框圆角等,并设置了滚动条的样式。最后,在JavaScript文件main.js中添加了一个setCategoryBarActive函数,用于根据当前页面路径自动激活相应的分类项。此外,还讨论了如何将自定义的分类栏集成到主题中,并提供了相关的Pug混入代码。整个过程确保了分类栏的可定制性和易维护性。","tags":["网站"],"categories":["建站手札"],"series":"webcustom","seriesOrder":null,"contentFormat":"text/markdown","content":"{% note warning %}\n注意:本文为早期 Hexo / Butterfly 魔改记录,本站现已迁移到 Astro,相关代码、路径或插件可能已经失效,仅作历史参考。\n{% endnote %}\n\n{% note warning 1 %}\n修改有风险,请注意备份\n{% endnote %}\n今天来自定义分类条\n# 添加pug\n新建文件`[BlogRoot]\\themes\\butterfly\\layout\\includes\\categoryBar.pug`文件,写入:\n```pug\n.category-bar-items#category-bar-items(class=is_home() ? 'home' : '')\n .category-bar-item(class=is_home() ? 'select' : '', id=\"category-bar-home\")\n a(href=url_for('/'))= __('博客首页')\n each item in site.categories.find({ parent: { $exists: false } }).data\n .category-bar-item(class=select ? (select === item.name ? 'select' : '') : '', id=item.name)\n a(href=url_for(item.path))= item.name\n .category-bar-item\n a(href=url_for('/archives/'))= __('文章存档')\ndiv.category-bar-right\n a.category-bar-more(href=url_for('/categories/'))= __('更多分类')\n```\n# 样式文件\n以上就是该滚动条的结构,下面我们开始实现样式的定义,新建文件`[BlogRoot]\\themes\\butterfly\\source\\css\\_layout\\category-bar.styl`写入以下文件:\n\n```scss\n#category-bar\n padding 7px 11px\n background var(--card-bg)\n border-radius 8px\n display flex\n white-space nowrap\n overflow hidden\n transition 0.3s\n height 50px\n width 100%\n justify-content space-between\n user-select none\n align-items center\n margin-bottom 20px\n\n .category-bar-right\n display flex\n border-radius 8px\n align-items center\n\n .category-bar-more\n margin-left 4px\n margin-right 4px\n font-weight 700\n border-radius 8px\n padding 0 8px\n\n .category-bar-items\n width 100%\n white-space nowrap\n overflow-x scroll\n scrollbar-width: none\n -ms-overflow-style: none\n overflow-y hidden\n display flex\n border-radius 8px\n align-items center\n height 30px\n\n &::-webkit-scrollbar\n display: none\n\n .category-bar-item\n a\n padding .1rem .5rem\n margin-right 6px\n font-weight 700\n border-radius 8px\n display flex\n align-items center\n height 30px\n\n &.select\n a\n background #3eb8be\n color var(--btn-color)\n```\n# 添加js\n打开`[BlogRoot]\\themes\\butterfly\\source\\js\\main.js`,添加js函数,`refreshFn`函数的上面:\n```js\n const setCategoryBarActive = () => {\n const categoryBar = document.querySelector(\"#category-bar\");\n const currentPath = decodeURIComponent(window.location.pathname);\n const isHomePage = currentPath === GLOBAL_CONFIG.root;\n\n if (categoryBar) {\n const categoryItems = categoryBar.querySelectorAll(\".category-bar-item\");\n categoryItems.forEach(item => item.classList.remove(\"select\"));\n\n const activeItemId = isHomePage ? \"category-bar-home\" : currentPath.split(\"/\").slice(-2, -1)[0];\n const activeItem = document.getElementById(activeItemId);\n\n if (activeItem) {\n activeItem.classList.add(\"select\");\n }\n }\n};\n```\n引用函数\n```js\nwindow.refreshFn = function () {\n initAdjust()\n\n if (GLOBAL_CONFIG_SITE.isPost) {\n GLOBAL_CONFIG.noticeOutdate !== undefined && addPostOutdateNotice()\n GLOBAL_CONFIG.relativeDate.post && relativeDate(document.querySelectorAll('#post-meta time'))\n } else {\n GLOBAL_CONFIG.relativeDate.homepage && relativeDate(document.querySelectorAll('#recent-posts time'))\n GLOBAL_CONFIG.runtime && addRuntime()\n addLastPushDate()\n toggleCardCategory()\n setCategoryBarActive() // 自己加的,用于切换类别栏目\n }\n```\n# 添加到主题\n修改`[BlogRoot]\\themes\\butterfly\\layout\\includes\\mixins\\indexPostUI.pug`,添加\n```diff\nmixin indexPostUI()\n - const indexLayout = theme.index_layout\n - const masonryLayoutClass = (indexLayout === 6 || indexLayout === 7) ? 'masonry' : ''\n #recent-posts.recent-posts.nc(class=masonryLayoutClass)\n+ #category-bar.category-bar\n+ include ../categoryBar.pug \n .recent-post-items\n```\n# 参考\n{% link liushen博客,Liushen,https://blog.liushen.fun/posts/a64defb4/ %}"}