更新日期:2022年8月1日
css样式表中已经删去第13行,现在可以直接复制。
修改部分提示内容。
警告
原创作品,谢绝未在文章开头标注原文链接及作者的转载。
前言
本文章并不适合小白,如果你没有基础请自行离开。
提示
-
教程基于matery主题。
-
涉及对主题源文件的修改,请注意备份。
-
请熟知主题文件树结构。
-
因使用本教程造成的损失本人概不负责。
:::
一、思路
二、动手环节
1、新建文件部分
cast.ejs
在layout/_partial下新建cast.ejs,内容:
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
| <%- css('css/cast') %> <!-- 引入css,你也可以将其放在head.ejs中,建议放在此处,放在head.ejs中时,当theme.csat.enable为false时,nav会有2em的空白 --> <div class="cast" id="cast"> <% if( theme.cast.icon.enable ) { %> <% if (theme.cast.icon.class) { %> <i id="casticon" class="<%- theme.cast.icon.class %>"></i> <% } else { %> <i id="casticon" class="fa-solid fa-bell"></i> <% } %> <% } %> <!-- 4-10 行判断是否使用自定义的fa图标,如果配置文件中留空则使用默认图标 --> <% if (theme.cast.date) { %> <p class="castdate"><%- theme.cast.date %></p> <% } %> <!-- 上面三行判断是否显示公告时间 --> <marquee class="casttext"><p class="casttextin" title="点击查看更多"><%- theme.cast.text %></p></marquee> <!-- 下面的代码判断配置文件中是否填写url,若填写,则插入点击跳转的事件 --> <% if(theme.cast && theme.cast.url){ %> <script> $(".casttextin").click(function(){ window.open('<%- theme.cast.url %>',"_self"); }) </script> <% } %> </div> <!-- 下面的代码判断是否设置自定义图标颜色 --> <% if( theme.cast.icon.color ){ %> <Style>#casticon{color: <%- theme.cast.icon.color %> ;}</Style> <% } else { %> <Style>#casticon{color: white;}</Style> <% } %>
|
cast.css
在 source/css 文件夹下新建 cast.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
| nav{ height: calc(64px + 2em); }
.cast{ display: flex; align-items: center; height: 2em; margin: 0; padding: 0.5em; } #casticon{ display: inline-block; height: 1em; } .casttext{ direction:left; } .castdate{ text-overflow:ellipsis; white-space: nowrap; } .cast > *{ padding: 0 4px; } .casttextin{ cursor: pointer; }
|
2、修改部分
修改 header.ejs 使所有页面出现滚动通知,在第二行和第三行之间添加:
1 2 3 4
| <% if (theme.cast && theme.cast.enable) { %> <%- partial('_partial/cast') %> <% } %> <!-- 只有当主题配置文件中包含cast字段和cast.enable的值为true时才引入cast.ejs模板 -->
|
注意
请注意HTML的嵌套关系,例如对于matery主题,添加的内容应该位于class为 bg-color nav-transparent 的div内,且在class为 nav-wrapper container 的div之前,如我的header.ejs修改后:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <header class="navbar-fixed"> <nav id="headNav" class="bg-color nav-transparent"> <!-- 下面三行为添加内容 --> <% if (theme.cast && theme.cast.enable) { %> <%- partial('_partial/cast') %> <% } %> <div id="navContainer" class="nav-wrapper container"> <div class="brand-logo"> <a href="<%- url_for() %>" class="waves-effect waves-light"> <% if (theme.logo !== undefined && theme.logo.length > 0) { %> <img src="<%- theme.jsDelivr.url %><%- url_for(theme.logo) %>" class="logo-img" alt="LOGO"> <% } %> <span class="logo-span"><%= config.title %></span> </a> </div> <%- partial('_partial/navigation') %> </div> <% if (theme.githubLink && theme.githubLink.enable) { %> <%- partial('_partial/github-link') %> <% } %> </nav> </header>
|
因为使用了资源压缩插件,无需在意空格,如果没有安装插件,请注意减少留空。
_config.yml
在主题配置文件尾部追加如下内容:
1 2 3 4 5 6 7 8 9 10
| cast: enable: true icon: enable: true class: color: date: 2022年7月11日 text: 修改本站大部分图片资源由png转为webp,提高加载速度。 url: './../cast/'
|