大家在使用 hexo 博客的 Next 主题的时候应该都觉得原来默认的归档页面很丑吧,最近也有小伙伴问我这个归档页面美化怎么弄的,今天就小小的总结一下。
效果如上面的页面所示,其实就是在原来默认的基础上修改了 css 样式,但是这个 css 样式修改不能直接使用浏览器 F12 查找元素修改,因为这个页面的 class 属性同样绑定了首页的元素,如果直接修改这个页面的样式那么首页的样式也会乱套,所以我们只能单独对这个页面的 class 属性名称做一些修改,然后再修改 class 属性对应的 css 样式。
好吧,上面的解释可能听起来不太懂,没关系,直接按照下面步骤修改就行了。
首先我们打开Next主题目录(注意这个美化样式只支持Next主题),然后找到 next/layout/_macro/post-collapse.swig
文件。
以下基于 Next5,其中原始内容如下,Next6 主题下类似:
//文件位置:~blog/next/layout/_macro/post-collapse.swig
{% macro render(post) %}
<article class="post post-type-{{ post.type | default('normal') }}" itemscope itemtype="http://schema.org/Article">
<header class="post-header">
<{% if theme.seo %}h3{% else %}h2{% endif %} class="post-title">
{% if post.link %}{# Link posts #}
<a class="post-title-link post-title-link-external" target="_blank" href="{{ url_for(post.link) }}" itemprop="url">
{{ post.title or post.link }}
<i class="fa fa-external-link"></i>
</a>
{% else %}
<a class="post-title-link" href="{{ url_for(post.path) }}" itemprop="url">
{% if post.type === 'picture' %}
{{ post.content }}
{% else %}
<span itemprop="name">{{ post.title | default(__('post.untitled')) }}</span>
{% endif %}
</a>
{% endif %}
</{% if theme.seo %}h3{% else %}h2{% endif %}>
<div class="post-meta">
<time class="post-time" itemprop="dateCreated"
datetime="{{ moment(post.date).format() }}"
content="{{ date(post.date, config.date_format) }}" >
{{ date(post.date, 'MM-DD') }}
</time>
</div>
</header>
</article>
{% endmacro %}
然后主要找 class 属性做修改,首先将 post-meta
代码块的内容上移到 post-header
下面,如下:
{% macro render(post) %}
<article class="post post-type-{{ post.type | default('normal') }}" itemscope itemtype="http://schema.org/Article">
<header class="post-header">
+ <div class="post-meta">
+ <time class="post-time" itemprop="dateCreated"
+ datetime="{{ moment(post.date).format() }}"
+ content="{{ date(post.date, config.date_format) }}" >
+ {{ date(post.date, 'MM-DD') }}
+ </time>
+ </div>
<{% if theme.seo %}h3{% else %}h2{% endif %} class="post-title">
{% if post.link %}{# Link posts #}
<a class="post-title-link post-title-link-external" target="_blank" href="{{ url_for(post.link) }}" itemprop="url">
{{ post.title or post.link }}
<i class="fa fa-external-link"></i>
</a>
{% else %}
<a class="post-title-link" href="{{ url_for(post.path) }}" itemprop="url">
{% if post.type === 'picture' %}
{{ post.content }}
{% else %}
<span itemprop="name">{{ post.title | default(__('post.untitled')) }}</span>
{% endif %}
</a>
{% endif %}
</{% if theme.seo %}h3{% else %}h2{% endif %}>
- <div class="post-meta">
- <time class="post-time" itemprop="dateCreated"
- datetime="{{ moment(post.date).format() }}"
- content="{{ date(post.date, config.date_format) }}" >
- {{ date(post.date, 'MM-DD') }}
- </time>
- </div>
</header>
</article>
{% endmacro %}
然后对照下面代码修改 class 属性,红色代码为原始代码,绿色代码为修改后的代码,实际上修改的地方只是在对应的 class 属性前面加上 my-
即可,比如原始是 post-title-link
,修改为 my-post-title-link
。注意以下只是原始代码和修改代码参考对比,不要直接复制!
{% macro render(post) %}
- <article class="post post-type-{{ post.type | default('normal') }}" itemscope itemtype="http://schema.org/Article">
+ <article class="my-post post-type-{{ post.type | default('normal') }}" itemscope itemtype="http://schema.org/Article">
- <header class="post-header">
+ <header class="my-post-header">
- <div class="post-meta">
+ <div class="my-post-meta">
- <time class="post-time" itemprop="dateCreated"
+ <time class="my-post-time" itemprop="dateCreated"
datetime="{{ moment(post.date).format() }}"
content="{{ date(post.date, config.date_format) }}" >
{{ date(post.date, 'MM-DD') }}
</time>
</div>
- <{% if theme.seo %}h3{% else %}h2{% endif %} class="post-title">
+ <{% if theme.seo %}h3{% else %}h2{% endif %} class="my-post-title">
{% if post.link %}{# Link posts #}
- <a class="post-title-link post-title-link-external" target="_blank" href="{{ url_for(post.link) }}" itemprop="url">
+ <a class="my-post-title-link post-title-link-external" target="_blank" href="{{ url_for(post.link) }}" itemprop="url">
{{ post.title or post.link }}
<i class="fa fa-external-link"></i>
</a>
{% else %}
- <a class="post-title-link" href="{{ url_for(post.path) }}" itemprop="url">
+ <a class="my-post-title-link" href="{{ url_for(post.path) }}" itemprop="url">
{% if post.type === 'picture' %}
{{ post.content }}
{% else %}
<span itemprop="name">{{ post.title | default(__('post.untitled')) }}</span>
{% endif %}
</a>
{% endif %}
</{% if theme.seo %}h3{% else %}h2{% endif %}>
</header>
</article>
{% endmacro %}
最后打开主题目录下的 next/source/css/_custom/custom.styl
文件,在文件末尾添加如下样式:
//文件位置:~blog/next/source/css/_custom/custom.styl
/*归档页样式优化*/
.my-post-time{
position: absolute;
color: #fff;
background-color: #49b1f5;
border-radius: 5px;
padding-left: 5px;
padding-right: 5px;
margin-left: 15px;
}
.mypost{
position: relative;
margin-bottom: 1rem;
-webkit-transition: all .2s ease-in-out;
-moz-transition: all .2s ease-in-out;
-o-transition: all .2s ease-in-out;
-ms-transition: all .2s ease-in-out;
transition: all .2s ease-in-out;
}
a.my-post-title-link:before{
top: 10px;
width: 18px;
height: 18px;
content: "\f073";
font: normal normal normal 14px/1 FontAwesome;
font-size: 18px;
line-height: 18px;
}
a.my-post-title-link{
text-decoration: none;
font-size: 18px;
font-weight: 400;
}
.my-post-title{
display: block;
margin-left: 5rem;
color: #4c4948;
text-decoration: none;
font-size: .8rem;
cursor: pointer;
}
.my-post-header{
position: top;
margin-bottom: 1rem;
-webkit-transition: all .2s ease-in-out;
-moz-transition: all .2s ease-in-out;
-o-transition: all .2s ease-in-out;
-ms-transition: all .2s ease-in-out;
transition: all .2s ease-in-out;
}
.my-post-title-link{
font-size: 16px;
font-weight: 500;
}
.my-post-meta{
position: absolute;
color: #99a9bf;
width: 80px;
color: #114142;
}
然后 hexo clean && hexo g && hexo s
就可以查看效果了!
本文由 Sanarous 创作,如果您觉得本文不错,请随意赞赏
采用 知识共享署名4.0 国际许可协议进行许可,转载前请务必署名
本文链接:https://bestzuo.cn/posts/3746574423.html
最后更新于:2020-07-03 17:47:30
评论