7.主题魔改:侧滑气泡式评论区
.webp)
AI-摘要
Tianli GPT
AI初始化中...
介绍自己 🙈
生成本文简介 👋
推荐相关文章 📖
前往主页 🏠
前往爱发电购买
7.主题魔改:侧滑气泡式评论区
Prorise7.主题魔改:侧滑气泡式评论区
前言:功能介绍与重要提示
本指南将,引导您实现一个功能与美化并存的“侧滑气泡式”评论区。最终效果为:
- 点击右下角的评论按钮后,整个评论区会从侧边平滑地滑出,覆盖部分页面。
- 滑出的评论区内,每一条评论都将以类似QQ或微信的“聊天气泡”样式展示。
警告: 这是一项涉及多个文件修改的“魔改”操作。在开始前,强烈建议您备份整个
themes/anzhiyu
文件夹,以便在出现问题时可以随时恢复。
第一步:创建集成的自定义CSS文件
为了方便管理,我们将把“侧滑面板”和“气泡样式”这两种美化所需的全部CSS代码,都放入一个新建的文件中。
- 创建CSS文件
- 在
themes/anzhiyu/source/css/
目录下,新建一个文件,命名为custom-comment.css
。
- 粘贴CSS代码
- 将下面的整合并优化过的CSS代码,完整复制到您刚创建的
custom-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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306/* 自定义twikoo评论输入框高度 */
.tk-input[data-v-619b4c52] .el-textarea__inner {
height: 130px ;
}
/* 输入评论时自动隐藏输入框背景图片 */
.tk-input[data-v-619b4c52] .el-textarea__inner:focus {
background-image: none ;
}
.tk-row {
flex:1;
display: flex;
flex-direction: row;
justify-content: flex-start ;
}
.tk-tag-green {
color: #fff ;
background-color: #52616b ;
border-color: #1e2022 ;
}
.tk-row:has(.tk-tag-green) {
justify-content: flex-end ;
}
.tk-extras:has(.tk-tag-green) {
justify-content: flex-end ;
}
/* 头像样式调整 */
.tk-avatar {
border-radius: 50% ;
width: 2.5rem ;
height: 2.5rem ;
}
.tk-avatar-img {
width: 2.5rem ;
height: 2.5rem ;
border-radius: 50% ;
}
/* 评论内容气泡化样式 */
.tk-content {
background: #c9d6df ; /* 访客气泡配色 */
color: #1e2022 ; /* 访客气泡字体配色 */
border-radius: 10px ;
padding: 8px 12px ;
margin: 3px 0 ;
position: relative ;
width: fit-content ;
max-width: 80% ;
font-size: 16px ;
line-height: 0.4 ;
word-wrap: break-word ;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1) ;
}
/* 访客气泡小三角 */
.tk-content::before {
content: '' ;
position: absolute ;
top: 15px ;
left: -8px ;
width: 0 ;
height: 0 ;
border-top: 8px solid transparent ;
border-right: 8px solid #c9d6df ;
border-bottom: 8px solid transparent ;
border-left: 0 ;
}
/* 访客气泡装饰小角 */
.tk-content::after {
content: '' ;
position: absolute ;
top: -5px ;
right: 10px ;
width: 0 ;
height: 0 ;
border-left: 5px solid transparent ;
border-right: 5px solid transparent ;
border-bottom: 5px solid #c9d6df ;
}
/* 博主评论样式 */
.tk-master .tk-content {
background: #52616b ; /* 博主气泡配色 */
color: #f0f5f9 ; /* 博主气泡字体配色 */
margin-left: auto ;
margin-right: 0 ;
max-width: 65% ;
line-height: 0.4 ;
}
/* 博主气泡小三角 */
.tk-master .tk-content::before {
left: auto ;
right: -8px ;
border-left: 8px solid #52616b ;
border-right: 0 ;
}
/* 博主气泡装饰小角 */
.tk-master .tk-content::after {
right: auto ;
left: 10px ;
border-bottom-color: #52616b ;
}
/* 超链接样式 */
.tk-content a {
color: #52616b ;
text-decoration: underline ;
}
.tk-master .tk-content a {
color: #c9d6df ;
}
/* 图片样式修复 */
.tk-content img {
max-width: 100% ;
border-radius: 5px ;
}
/* 代码块样式修复 */
.tk-content pre {
white-space: pre-wrap ;
word-wrap: break-word ;
background: rgba(0, 0, 0, 0.1) ;
padding: 8px ;
border-radius: 5px ;
}
/* 楼中楼回复样式 */
.tk-replies {
margin-left: 40px ;
margin-top: 10px ;
border-left: 2px solid #e0e0e0 ;
padding-left: 20px ;
}
/* 评论布局调整 */
.tk-comment {
margin-bottom: 20px ;
}
.tk-main {
margin-left: 10px ;
}
/* 博主评论右对齐 */
.tk-master .tk-main {
margin-left: 0 ;
margin-right: 10px ;
text-align: right ;
}
.tk-master .tk-avatar {
order: 2 ;
}
/* 元信息样式 */
.tk-extras {
opacity: 0.6 ;
font-size: 12px ;
margin-top: 5px ;
transition: opacity 0.3s ease ;
}
.tk-content:hover + .tk-extras,
.tk-extras:hover {
opacity: 1 ;
}
/* 夜间模式配色 */
[data-theme="dark"] .tk-content {
background: #1e2022 ;
color: #f0f5f9 ;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3) ;
}
[data-theme="dark"] .tk-content::before {
border-right-color: #1e2022 ;
}
[data-theme="dark"] .tk-content::after {
border-bottom-color: #1e2022 ;
}
[data-theme="dark"] .tk-master .tk-content {
background: #52616b ;
color: #f0f5f9 ;
}
[data-theme="dark"] .tk-master .tk-content::before {
border-left-color: #52616b ;
}
[data-theme="dark"] .tk-master .tk-content::after {
border-bottom-color: #52616b ;
}
[data-theme="dark"] .tk-content a {
color: #c9d6df ;
}
[data-theme="dark"] .tk-master .tk-content a {
color: #f0f5f9 ;
}
[data-theme="dark"] .tk-replies {
border-left-color: #444 ;
}
/* 移动端适配 */
@media screen and (max-width: 768px) {
.tk-content {
max-width: 85% ;
}
.tk-master .tk-content {
max-width: 75% ;
}
.tk-replies {
margin-left: 20px ;
}
}
/* 大屏幕优化 */
@media screen and (min-width: 1024px) {
.tk-content {
max-width: 75% ;
}
.tk-master .tk-content {
max-width: 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 ;
left: 0;
top: 0;
width: 40%;
height: 100vh;
z-index: 89 ;
background: rgba(25, 25, 25, 0.3);
filter: blur(4px) ;
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);
} - 将下面的整合并优化过的CSS代码,完整复制到您刚创建的
第二步:创建侧滑功能JS文件
- 创建JS文件
- 在
themes/anzhiyu/source/js/
目录下,新建一个文件,命名为fixed_comment.js
。
- 粘贴JS代码
- 将下面的JavaScript代码完整复制到您刚创建的
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// 移除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(){
// 检查是否已存在蒙版,避免重复创建
if (document.getElementById('quit-board')) return;
var quitBoardHTML = `<div id="quit-board" onclick="RemoveFixedComment()"></div>`
var commentBoard = document.getElementById('post-comment');
// 确保评论区存在再操作
if (commentBoard) {
commentBoard.insertAdjacentHTML("beforebegin", quitBoardHTML);
}
}
function FixedCommentBtn(){
var commentBoard = document.getElementById('post-comment');
if (commentBoard) {
if (commentBoard.className.indexOf('fixedcomment') > -1){
RemoveFixedComment();
} else {
CreateQuitBoard();
AddFixedComment();
}
} else {
// 如果当前页面没有评论区,则跳转到留言板页面
if (window.pjax){
pjax.loadUrl("/comments/#post-comment");
} else {
window.location.href = "/comments/#post-comment";
}
}
}
// 切换页面先初始化一遍,确保开始时是原生状态。所以要加pjax重载。
RemoveFixedComment(); - 将下面的JavaScript代码完整复制到您刚创建的
第三步:修改侧边栏按钮模板
我们需要修改右下角“直达评论”按钮的行为,让它触发我们的侧滑功能。
找到目标文件:
themes/anzhiyu/layout/includes/rightside.pug
修改内容:
- 在文件中找到包含
a#to_comment
的那一行。 - 将其从一个普通的
<a>
标签,改成一个调用我们JS函数的<button>
。
修改指引:
1
2- 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();")(您只需将原来的那行替换为以
+
号开头的新行即可)- 在文件中找到包含
第四步:在主题配置中注入新文件
- 打开主题配置文件 (
themes/anzhiyu/_config.yml
)。 - 找到
inject:
配置项,添加我们新建的CSS和JS文件。1
2
3
4
5
6
7inject:
head:
# - 其他 head 内容
- '<link rel="stylesheet" href="/css/custom-comment.css">'
bottom:
# - 其他 bottom 内容
- '<script src="/js/fixed_comment.js"></script>'
评论
隐私政策
TwikooWaline
✅ 你无需删除空行,直接评论以获取最佳展示效果