右下角菜单打开评论板块

本文修改主题为Butterfly主题,若使用其他主题,请自行确认修改位置。

原文出处

https://akilar.top/posts/397b8b90/

效果预览

查看图片

效果预览

修改步骤

1. 新建文件

新建[Blogroot]\themes\butterfly\source\css\custom\fixed_comment.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
div#post-comment.fixedcomment {
position: fixed;
top: 0;
width: 60%;
right: 0;
padding: 25px 30px 20px 20px;
height: 100vh;
overflow: scroll;
z-index: 90;
background: rgba(222, 222, 222, 0.95);
box-shadow:3px 2px 14px #464340;
animation: fixedright 0.5s linear;
}
div#post-comment.fixedcomment::-webkit-scrollbar {
width: 0;
}
div#quit-board{
display: none;
}
div#quit-board.fixedcomment {
position: fixed;
display:block!important;
left: 0;
top: 0;
width: 40%;
height: 100vh;
z-index: 89!important;
background: rgba(25,25,25,0.3);
filter: blur(4px) !important;
animation: fixedleft 0.5s linear;
}
/*手机端样式适配*/
@media screen and (max-width: 768px) {
div#post-comment.fixedcomment {
width: 90%;
right: 0;
}
div#quit-board.fixedcomment {
width: 10%;
}
}
/*动画效果*/
@keyframes fixedright {
from {right:-50%;}
to {right:0;}
}
@keyframes fixedleft {
from {left:-50%;}
to {left:0;}
}
/* 夜间模式匹配 */
[data-theme="dark"]
div#post-comment.fixedcomment {
background: rgba(35, 35, 35, 0.95);
box-shadow:3px 2px 12px #90a1a4;
}
[data-theme="dark"]
div#quit-board.fixedcomment {
background: rgba(147, 146, 128, 0.3);
}

新建[Blogroot]\themes\butterfly\source\js\custom\fixed_comment.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
//移除FixedComment类,保持原生样式,确保不与最新评论跳转冲突
function RemoveFixedComment() {
var activedItems = document.querySelectorAll('.fixedcomment');
if (activedItems) {
for (i = 0; i < activedItems.length; i++) {
activedItems[i].classList.remove('fixedcomment');
}
}
}
//给post-comment添加fixedcomment类
function AddFixedComment(){
var commentBoard = document.getElementById('post-comment');
var quitBoard = document.getElementById('quit-board');
commentBoard.classList.add('fixedcomment');
quitBoard.classList.add('fixedcomment');
}
//创建一个蒙版,作为退出键使用
function CreateQuitBoard(){
var quitBoard = `<div id="quit-board" onclick="RemoveFixedComment()"></div>`
var commentBoard = document.getElementById('post-comment');
commentBoard.insertAdjacentHTML("beforebegin",quitBoard)
}

function FixedCommentBtn(){
//第一步,判断当前是否存在FixedComment类,存在则移除,不存在则添加
// 获取评论区对象
var commentBoard = document.getElementById('post-comment');
// 若评论区存在
if (commentBoard) {
// 判断是否存在fixedcomment类
if (commentBoard.className.indexOf('fixedcomment') > -1){
// 存在则移除
RemoveFixedComment();
}
else{
// 不存在则添加
CreateQuitBoard();
AddFixedComment();
}
}
// 若不存在评论区则跳转至留言板(留言板路径记得改为自己的)
else{
// 判断是否开启了pjax,尽量不破坏全局吸底音乐刷新
if (pjax){
pjax.loadUrl("/comments/#post-comment");
}
else{
window.location.href = "/comments/#post-comment";
}
}
}
//切换页面先初始化一遍,确保开始时是原生状态。所以要加pjax重载。
RemoveFixedComment();

2. 修改文件

修改[Blogroot]\_config.butterfly.ymlinject配置项,添加引入的js和css

注意:fixed_comment.js 有前置依赖且需要排除 pjax ,故插入 js 时应当加入 data-pjax 和 defer 标签

1
2
3
4
5
  inject:
head:
+ - <link rel="stylesheet" href="/css/custom/fixed_comment.css" media="defer" onload="this.media='all'">
bottom:
+ - <script data-pjax defer src="/js/custom/fixed_comment.js"></script>

修改[Blogroot]\themes\butterfly\layout\includes\rightside.pug,大约第36行的位置

1
2
3
  if commentsJsLoad
- a#to_comment(href="#post-comment" title=_p("rightside.scroll_to_comment"))
+ button#to_comment(type="button" title=_p("rightside.scroll_to_comment") onclick="FixedCommentBtn();")