forked from Lete114/hexo-artitalk-static
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ejs
314 lines (298 loc) · 8.76 KB
/
main.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
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
307
308
309
310
311
312
313
314
<% (Array.isArray(data.imports.before) ? data.imports.before : []).forEach(function(source, i){ %>
<%- source %>
<% }) %>
<style>
.at_items {
padding: 0;
list-style: none;
}
.at_item {
display: flex;
}
.at_avatar {
border: 1px solid #ddd;
padding: 2px;
float: left;
border-radius: 64px;
transition: all 1s;
width: 48px;
height: 48px;
}
.at_content_container {
margin: 0 0 20px 20px;
position: relative;
padding: 10px;
border-radius: 5px;
background: var(--at-color, #9bcd9b);
}
.at_content_container::before {
content: '';
position: absolute;
top: 20px; /* 将箭头位置垂直居中 */
left: -10px; /* 设置箭头的左侧位置 */
border-top: 10px solid transparent;
border-bottom: 10px solid transparent;
border-right: 10px solid var(--at-color, #9bcd9b); /* 设置箭头的样式和颜色 */
transform: translateY(-50%); /* 将箭头位置垂直居中 */
}
.at_item:nth-child(odd) .at_content_container {
--at-color: #ff9e77;
}
.at_content {
white-space: pre-wrap;
}
.at_time {
margin-top: 10px;
font-size: 12px;
}
.at_more {
display: flex;
justify-content: center;
margin: 16px 0 10px;
}
.at_more_btn {
opacity: 0.8;
width: auto;
min-width: 80px;
height: 36px;
border: none;
color: #fff;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
padding: 8px 16px;
line-height: 20px;
font-weight: 600;
font-size: 12px;
border-radius: 12px;
background-color: #f4645f;
}
.at_more_btn[disabled] {
opacity: 0.5;
cursor: not-allowed;
cursor: no-drop;
}
.at_loading {
animation: at_loading 0.8s linear infinite;
}
.at_more_loading {
display: none;
height: inherit;
}
@keyframes at_loading {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>
<% const items = artitalk.splice(data.pageSize) %>
<div id="at_main">
<ul class="at_items">
<% artitalk.forEach(item => { %>
<li class="at_item">
<img class="at_avatar" alt="avatar" />
<div class="at_content_container">
<div class="at_content"><%- item.content %></div>
<div class="at_time"><%= formatDate(item.timestamp) %></div>
</div>
</li>
<%});%>
</ul>
<div class="at_more">
<button class="at_more_btn">加载更多</button>
<svg class="at_more_loading at_loading" viewBox="0 0 100 100">
<circle cx="50" cy="50" fill="none" stroke="currentColor" stroke-width="8" r="40" stroke-linecap="round" stroke-dasharray="128 120"></circle>
</svg>
</div>
</div>
<script data-pjax="">
var pjaxFlag = false; //pjax加载标识
var at_more_btn_click; // 在全局作用域声明 at_more_btn_click
function initPage() {
var pageSize = <%= data.pageSize %>;
var artitalk = <%-JSON.stringify(items)%>;
var at_more = document.querySelector('.at_more');
if (at_more) { // 确保at_more元素存在
at_more.style.display = artitalk.length ? '' : 'none';
}
var avatar = document.querySelectorAll('.at_avatar');
avatar.forEach((el) => {
el.src = '<%-data.logo%>';
});
<%if(data.avatar){%>
var img = new Image();
img.src = '<%= data.avatar %>';
img.style.display = 'none';
document.body.appendChild(img);
img.onload = function() {
document.body.removeChild(img);
avatar.forEach((el) => {
el.src = img.src;
});
};
<%}%>
var endLi = document.querySelectorAll('.at_item');
endLi = endLi[endLi.length - 1];
var at_more_btn = document.querySelector('.at_more_btn');
if (at_more_btn) { // 确保at_more_btn元素存在
var at_more_btn_text = at_more_btn.textContent;
var at_more_loading = document.querySelector('.at_more_loading').cloneNode(true);
at_more_loading.style.display = 'unset';
at_more_btn_click = function() { // 在这里定义 at_more_btn_click
if (at_more_btn) {
at_more_btn.setAttribute('disabled', '');
at_more_btn.innerHTML = at_more_loading.outerHTML;
setTimeout(() => {
for (const item of artitalk.splice(0, pageSize)) {
const nLi = endLi.cloneNode(true);
nLi.querySelector('.at_content').innerHTML = item.content;
const date = new Date(item.timestamp);
nLi.querySelector('.at_time').innerHTML = date.toLocaleString();
endLi.parentElement.appendChild(nLi);
endLi = nLi;
}
at_more_btn.innerHTML = at_more_btn_text;
at_more_btn.removeAttribute('disabled');
// 这里再次确保at_more存在
if (at_more) {
at_more.style.display = artitalk.length ? '' : 'none';
}
}, 500);
}
}
if(at_more_btn){
at_more_btn.addEventListener('click', at_more_btn_click);
}
}
}
document.addEventListener('pjax:success', function() {
pjaxFlag = true;
//应用样式表
let styleContent = `
.at_items {
padding: 0;
list-style: none;
}
.at_item {
display: flex;
}
.at_avatar {
border: 1px solid #ddd;
padding: 2px;
float: left;
border-radius: 64px;
transition: all 1s;
min-width: 48px;
height: 48px;
}
.at_content_container {
margin: 0 0 20px 20px;
position: relative;
padding: 10px;
border-radius: 5px;
background: var(--at-color, rgba(219, 255, 247, 0.5));
box-shadow: 0 0 12px rgba(0, 0, 0, 0.2);
}
.at_content_container::before {
content: '';
position: absolute;
top: 20px; /* 将箭头位置垂直居中 */
left: -10px; /* 设置箭头的左侧位置 */
border-top: 10px solid transparent;
border-bottom: 10px solid transparent;
border-right: 10px solid var(--at-color, rgba(219, 255, 247, 0.5)); /* 设置箭头的样式和颜色 */
transform: translateY(-50%); /* 将箭头位置垂直居中 */
}
.at_item:nth-child(odd) .at_content_container {
--at-color: rgba(255, 219, 227, 0.5);
}
.at_content {
white-space: pre-wrap;
}
.at_time {
margin-top: 10px;
font-size: 12px;
}
.at_more {
display: flex;
justify-content: center;
margin: 16px 0 10px;
}
.at_more_btn {
opacity: 0.8;
width: auto;
min-width: 80px;
height: 36px;
border: none;
color: #fff;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
padding: 8px 16px;
line-height: 20px;
font-weight: 600;
font-size: 12px;
border-radius: 12px;
background-color: #f4645f;
}
.at_more_btn[disabled] {
opacity: 0.5;
cursor: not-allowed;
cursor: no-drop;
}
.at_loading {
animation: at_loading 0.8s linear infinite;
}
.at_more_loading {
display: none;
height: inherit;
}
@keyframes at_loading {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
`;
// 检查特定的<style>标签是否已经存在
let customStyleTag = document.getElementById('custom-style');
if (!customStyleTag) {
customStyleTag = document.createElement('style');
customStyleTag.type = 'text/css';
customStyleTag.id = 'custom-style';
// 将<style>元素添加到<head>
document.head.appendChild(customStyleTag);
}
// 将CSS样式内容注入到<style>标签中
if (customStyleTag.styleSheet) { // 针对IE
customStyleTag.styleSheet.cssText = styleContent;
} else {
// 清除之前的样式
while (customStyleTag.firstChild) {
customStyleTag.removeChild(customStyleTag.firstChild);
}
// 添加新的样式
customStyleTag.appendChild(document.createTextNode(styleContent));
}
//清除监听器,避免重复绑定
var at_more_btn = document.querySelector('.at_more_btn');
if(at_more_btn){
at_more_btn.removeEventListener('click', at_more_btn_click);
}
initPage();
});
//未通过pjax加载时初始化
if (!pjaxFlag) {
initPage();
}
</script>
<% (Array.isArray(data.imports.after) ? data.imports.after : []).forEach(function(bottom, i){ %>
<%- bottom %>
<% }) %>