安知鱼主题默认幻灯片切换的样式美化

作为一个博客也好,网站也罢,个人观点,幻灯片展示内容板块必不可少,安知鱼主题自带一个幻灯片切换效果,需要在主题配置文件中打开。

1.开启 swiper 幻灯片

在安知鱼主题的 _config.yml 配置文件中,搜索 swiper,会找到如下代码

1
2
3
4
swiper:
enable: false
swiper_css: https://npm.elemecdn.com/anzhiyu-theme-static@1.0.0/swiper/swiper.min.css #swiper css依赖
swiper_js: https://npm.elemecdn.com/anzhiyu-theme-static@1.0.0/swiper/swiper.min.js #swiper js依赖

将里面 false 修改为 true,开启swiper幻灯片

1
2
3
4
swiper:
enable: true
swiper_css: https://npm.elemecdn.com/anzhiyu-theme-static@1.0.0/swiper/swiper.min.css #swiper css依赖
swiper_js: https://npm.elemecdn.com/anzhiyu-theme-static@1.0.0/swiper/swiper.min.js #swiper js依赖

2.CSS样式修改

/themes/anzhiyu/source/css_extra/home_top/ 文件夹中,找到 swiperstyle.css 样式文件,这个文件调控着默认幻灯片的样式,直接复制下面的代码替换掉源代码,就可以实现我的截图效果,不需要额外引入 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#swiper_container_blog {
width: 49%;
transition: all 0.3s, width 0s;
border-radius: 12px;
}
.swiper-wrapper {
align-items: center;
}
div#swiper_container {
transition: 0.3s;
}
/* 防止轮播图css未加载完时的超度横行滚动条 */
#swiper_container {
overflow: hidden !important;
}
div.swiper_container_card {
margin: 1rem auto 0;
}
@media screen and (max-width: 768px) {
div.swiper_container_card {
margin: 0.2rem auto 0;
}
}
div#swiper_container {
background: rgba(255, 255, 255, 0);
width: 100%;
padding: 0;
overflow: initial;
background: var(--card-bg);
border-radius: 12px;
border: var(--style-border);
}
div#swiper_container:hover {
border: var(--style-border-hover);
box-shadow: var(--anzhiyu-shadow-main);
}
.blog-slider {
width: 100%;
position: relative;
border-radius: 12px 8px 8px 12px;
margin: 0 auto;
}
.blog-slider__item {
display: -webkit-box;
display: -moz-box;
display: -webkit-flex;
display: -ms-flexbox;
display: box;
display: flex;
-webkit-box-align: center;
-moz-box-align: center;
-o-box-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
}
.blog-slider__item.swiper-slide-active .blog-slider__img img {
opacity: 1;
-ms-filter: none;
filter: none;
-webkit-transition-delay: 0.3s;
-moz-transition-delay: 0.3s;
-o-transition-delay: 0.3s;
-ms-transition-delay: 0.3s;
transition-delay: 0.3s;
}
.blog-slider__item.swiper-slide-active .blog-slider__content > * {
opacity: 1;
-ms-filter: none;
filter: none;
-webkit-transform: none;
-moz-transform: none;
-o-transform: none;
-ms-transform: none;
transform: none;
}
.blog-slider__item.swiper-slide-active .blog-slider__content > :nth-child(1) {
-webkit-transition-delay: 0.3s;
-moz-transition-delay: 0.3s;
-o-transition-delay: 0.3s;
-ms-transition-delay: 0.3s;
transition-delay: 0.3s;
}
.blog-slider__item.swiper-slide-active .blog-slider__content > :nth-child(2) {
-webkit-transition-delay: 0.4s;
-moz-transition-delay: 0.4s;
-o-transition-delay: 0.4s;
-ms-transition-delay: 0.4s;
transition-delay: 0.4s;
}
.blog-slider__item.swiper-slide-active .blog-slider__content > :nth-child(3) {
-webkit-transition-delay: 0.5s;
-moz-transition-delay: 0.5s;
-o-transition-delay: 0.5s;
-ms-transition-delay: 0.5s;
transition-delay: 0.5s;
}
.blog-slider__item.swiper-slide-active .blog-slider__content > :nth-child(4) {
-webkit-transition-delay: 0.6s;
-moz-transition-delay: 0.6s;
-o-transition-delay: 0.6s;
-ms-transition-delay: 0.6s;
transition-delay: 0.6s;
}
.blog-slider__item.swiper-slide-active .blog-slider__content > :nth-child(5) {
-webkit-transition-delay: 0.7s;
-moz-transition-delay: 0.7s;
-o-transition-delay: 0.7s;
-ms-transition-delay: 0.7s;
transition-delay: 0.7s;
}
.blog-slider__item.swiper-slide-active .blog-slider__content > :nth-child(6) {
-webkit-transition-delay: 0.8s;
-moz-transition-delay: 0.8s;
-o-transition-delay: 0.8s;
-ms-transition-delay: 0.8s;
transition-delay: 0.8s;
}
.blog-slider__item.swiper-slide-active .blog-slider__content > :nth-child(7) {
-webkit-transition-delay: 0.9s;
-moz-transition-delay: 0.9s;
-o-transition-delay: 0.9s;
-ms-transition-delay: 0.9s;
transition-delay: 0.9s;
}
.blog-slider__item.swiper-slide-active .blog-slider__content > :nth-child(8) {
-webkit-transition-delay: 1s;
-moz-transition-delay: 1s;
-o-transition-delay: 1s;
-ms-transition-delay: 1s;
transition-delay: 1s;
}
.blog-slider__item.swiper-slide-active .blog-slider__content > :nth-child(9) {
-webkit-transition-delay: 1.1s;
-moz-transition-delay: 1.1s;
-o-transition-delay: 1.1s;
-ms-transition-delay: 1.1s;
transition-delay: 1.1s;
}
.blog-slider__item.swiper-slide-active .blog-slider__content > :nth-child(10) {
-webkit-transition-delay: 1.2s;
-moz-transition-delay: 1.2s;
-o-transition-delay: 1.2s;
-ms-transition-delay: 1.2s;
transition-delay: 1.2s;
}
.blog-slider__item.swiper-slide-active .blog-slider__content > :nth-child(11) {
-webkit-transition-delay: 1.3s;
-moz-transition-delay: 1.3s;
-o-transition-delay: 1.3s;
-ms-transition-delay: 1.3s;
transition-delay: 1.3s;
}
.blog-slider__item.swiper-slide-active .blog-slider__content > :nth-child(12) {
-webkit-transition-delay: 1.4s;
-moz-transition-delay: 1.4s;
-o-transition-delay: 1.4s;
-ms-transition-delay: 1.4s;
transition-delay: 1.4s;
}
.blog-slider__item.swiper-slide-active .blog-slider__content > :nth-child(13) {
-webkit-transition-delay: 1.5s;
-moz-transition-delay: 1.5s;
-o-transition-delay: 1.5s;
-ms-transition-delay: 1.5s;
transition-delay: 1.5s;
}
.blog-slider__item.swiper-slide-active .blog-slider__content > :nth-child(14) {
-webkit-transition-delay: 1.6s;
-moz-transition-delay: 1.6s;
-o-transition-delay: 1.6s;
-ms-transition-delay: 1.6s;
transition-delay: 1.6s;
}
.blog-slider__item.swiper-slide-active .blog-slider__content > :nth-child(15) {
-webkit-transition-delay: 1.7s;
-moz-transition-delay: 1.7s;
-o-transition-delay: 1.7s;
-ms-transition-delay: 1.7s;
transition-delay: 1.7s;
}
.blog-slider__img {
width: 290px;
-webkit-flex-shrink: 0;
flex-shrink: 0;
height: 200px;
padding: 10px;
border-radius: 5px;
transform: translateX(0);
overflow: hidden;
}
.blog-slider__img:after {
content: "";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border-radius: 5px;
opacity: 0.8;
}
.blog-slider__img img {
width: 100%;
height: 100%;
object-fit: cover;
display: block;
opacity: 0;
border-radius: 5px;
transition: all 0.3s;
}
.blog-slider__content {
padding-right: 50px;
padding-left: 50px;
}
.blog-slider__content > * {
opacity: 0;
-webkit-transform: translateY(25px);
-moz-transform: translateY(25px);
-o-transform: translateY(25px);
-ms-transform: translateY(25px);
transform: translateY(25px);
-webkit-transition: all 0.4s;
-moz-transition: all 0.4s;
-o-transition: all 0.4s;
-ms-transition: all 0.4s;
transition: all 0.4s;
}
.blog-slider__code {
font-size: 24px;
font-weight: 700;
color: white;
margin-bottom: 0;
display: block;
text-align: center;
}
.blog-slider__title {
font-size: 24px;
font-weight: 700;
color: white;
text-align: center;
-webkit-line-clamp: 1;
display: -webkit-box;
overflow: hidden;
-webkit-box-orient: vertical;
}
.blog-slider__text {
font-size: 16px;
font-weight: 700;
text-align: center;
color: white;
line-height: 1.5em;
width: 100%;
height: 50px;
word-break: break-all;
word-wrap: break-word;
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
}
.blog-slider__button {
display: -webkit-inline-box;
display: -moz-inline-box;
display: -webkit-inline-flex;
display: -ms-inline-flexbox;
display: inline-box;
display: inline-flex;
background-color: var(--btn-bg);
padding: 4px 14px;
border-radius: 8px;
color: var(--btn-color);
text-decoration: none;
font-weight: 500;
-webkit-box-pack: center;
-moz-box-pack: center;
-o-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
text-align: center;
letter-spacing: 1px;
display: none;
}
.blog-slider__button:hover {
background-color: var(--btn-hover-color);
color: var(--btn-color);
}
.blog-slider .swiper-container-horizontal > .swiper-pagination-bullets,
.blog-slider .swiper-pagination-custom,
.blog-slider .swiper-pagination-fraction {
bottom: 10px;
left: 0;
width: 100%;
}
.blog-slider__pagination {
position: absolute;
z-index: 21;
right: auto !important;
width: auto !important;
text-align: center;
left: 50% !important;
top: 90%;
bottom: auto !important;
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
-o-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
.blog-slider .blog-slider__pagination.swiper-pagination-bullets .swiper-pagination-bullet {
margin: 0 8px;
}
.blog-slider__pagination .swiper-pagination-bullet {
width: 21px;
height: 12px;
display: inline-block;
border-radius: 10px;
background: #fbfafe;
opacity: 1;
-webkit-transition: all 0.3s;
-moz-transition: all 0.3s;
-o-transition: all 0.3s;
-ms-transition: all 0.3s;
transition: all 0.3s;
}
.blog-slider__pagination .swiper-pagination-bullet-active {
opacity: 1;
-ms-filter: none;
filter: none;
background: var(--btn-bg);
height: 12px;
}
@media screen and (max-width: 768px) {
.blog-slider__pagination {
-webkit-transform: translateX(-50%);
-moz-transform: translateX(-50%);
-o-transform: translateX(-50%);
-ms-transform: translateX(-50%);
transform: translateX(-50%);
left: 50% !important;
top: 320px;
width: 100% !important;
display: -webkit-box;
display: -moz-box;
display: -webkit-flex;
display: -ms-flexbox;
display: box;
display: flex;
-webkit-box-pack: center;
-moz-box-pack: center;
-o-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
-webkit-box-align: center;
-moz-box-align: center;
-o-box-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
}
.blog-slider .blog-slider__pagination.swiper-pagination-bullets .swiper-pagination-bullet {
margin: 0 5px;
}
.blog-slider__pagination .swiper-pagination-bullet-active {
height: 11px;
width: 30px;
}
.blog-slider__button {
display: -webkit-inline-box;
display: -moz-inline-box;
display: -webkit-inline-flex;
display: -ms-inline-flexbox;
display: inline-box;
display: inline-flex;
width: 100%;
}
.blog-slider {
min-height: 350px;
height: auto;
margin-bottom: 10px;
}
.blog-slider__content {
text-align: center;
padding: 0 30px;
}
.blog-slider__item {
-webkit-box-orient: vertical;
-moz-box-orient: vertical;
-o-box-orient: vertical;
-webkit-flex-direction: column;
-ms-flex-direction: column;
flex-direction: column;
}
.blog-slider__img {
width: 100%;
padding-top: 0;
}
.blog-slider__content {
padding-left: 10px;
padding-right: 10px;
margin-top: 15px;
}
.blog-slider__pagination.swiper-pagination-clickable.swiper-pagination-bullets {
top: 218px;
}

.swiper_container_card #swiper_container_blog div#swiper_container.blog-slider {
padding: 10px;
overflow: hidden;
}
.blog-slider__item .blog-slider__content .blog-slider__code {
margin-top: 10px;
}
.blog-slider__title {
font-size:16px;
font-weight:500;
color: white;
margin-top: 5px;
}
.blog-slider__code {
font-size:16px;
font-weight:500;
color: white;
margin-top: 5px;
}
.blog-slider__text {
font-size:16px;
font-weight:500;
color: white;
margin-top: 5px;
}
}
@media screen and (min-width: 768px) {
.blog-slider {
height: 200px;
}
.blog-slider__img {
height: 200px;
}
}

@media screen and (max-width: 1200px) {
div#swiper_container {
width: 100%;
}
#swiper_container_blog {
width: 99%;
}
}

.swiper_container_card {
display: flex !important;
justify-content: space-around;
flex-direction: row !important;
user-select: none;
}

主要是调整了 swiper 默认 分页器 的位置,原来的幻灯片分页器在右侧边栏上,我给他调整到了幻灯片底部。

3.修改幻灯片调用

默认幻灯片调用代码修改,需要在 /themes/anzhiyu/layout/includes/top/ 文件夹中,修改 top.pug 文件,涉及到文件对缩进有严格要求,可以使用下面的代码整体替换掉文件代码

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
- const getRandomGradient = () => `linear-gradient(${Math.floor(Math.random()*360)}deg, rgb(${Math.floor(Math.random()*255)},${Math.floor(Math.random()*255)},${Math.floor(Math.random()*255)}), rgb(${Math.floor(Math.random()*255)},${Math.floor(Math.random()*255)},${Math.floor(Math.random()*255)}))`
- const truncateText = (text, maxLength) => !text ? '' : (text.length > maxLength ? text.substring(0, maxLength) + ' ...' : text)
- let home_top_config = theme.home_top
- let { title, subTitle, siteText,default_descr } = home_top_config
if home_top_config.enable
#home_top
.swiper_container_card(style='height: auto;width: 100%')
#bannerGroup
#random-banner
if theme.peoplecanvas.enable
canvas#peoplecanvas
else
.banners-title
.banners-title-big=title
.banners-title-big=subTitle
.banners-title-small=siteText
include ../anzhiyu/tags-group-all.pug

a#random-hover(href='javascript:toRandomPost()')
i.anzhiyufont.anzhiyu-icon-paper-plane
.bannerText 随便逛逛
i.anzhiyufont.anzhiyu-icon-arrow-right
.categoryGroup
each item in home_top_config.category
.categoryItem(style=`box-shadow:${item.shadow}`)
a.categoryButton(href=url_for(item.path) class=`${item.class}`)
span.categoryButtonText=item.name
if (item.icon.startsWith("fa"))
i(class=`${item.icon}`)
else if (item.icon.startsWith("icon"))
svg.icon(aria-hidden="true")
use(xlink:href=`#`+ item.icon)
else if (item.icon.startsWith("anzhiyu"))
i.anzhiyufont(class=`${item.icon}`)
if theme.home_top.swiper.enable
#swiper_container_blog
#swiper_container.blog-slider.swiper-container-fade.swiper-container-horizontal
.blog-slider__wrp.swiper-wrapper(style='transition-duration: 0ms;')
//- 文章卡片
- let swiper_list = sort_attr_post("swiper_list")
each item in swiper_list
.blog-slider__item.swiper-slide(style=`width: 100%; height: 100%; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms; background: ${getRandomGradient()};`)
a.blog-slider__img(href=item.path, title=item.title)
img(width='48' height='48' src=item.cover alt='图片' onerror=`this.src=` + error_img + `; this.onerror = null;`)
.blog-slider__content
a.blog-slider__title(href=item.path, alt=item.title)= item.title
if (timemode === 'updated' )
span.blog-slider__code= item.updated.format('YYYY-MM-DD')
else
span.blog-slider__code= item.date.format('YYYY-MM-DD')
- const content = strip_html(item.content)
- let expert = content.substring(0, theme.index_post_content.length)
- content.length > theme.index_post_content.length ? expert += ' ...' : ''
.blog-slider__text= truncateText(item.description ? item.description : expert, 80)
a.blog-slider__button(href=item.path, alt=item.title) 详情
//- 轮播图组件
.blog-slider__pagination.swiper-pagination-clickable.swiper-pagination-bullets

#topPostGroup
- let top_group_list = sort_attr_post("top_group_list").splice(0, 4)
each item in top_group_list
.top-group-list-item
.post_cover.left_radius
a(href=item.path title=item.title)
span.top-group-text 荐
img.post_bg(alt=item.title, src=item.cover, onerror=`this.src=` + error_img + `; this.onerror = null;`)
.top-group-info
a.article-title(href=item.path title=item.title)=item.title
.top-group-list-none
.top-group-list-none
.top-group-list-none
else
.topGroup
- let top_group_list = sort_attr_post("top_group_list").splice(0, 6)
- let top_group_banner_tips = theme.home_top.banner.tips
- let top_group_banner_title = theme.home_top.banner.title
- let top_group_banner_image = theme.home_top.banner.image
- let top_group_banner_link = url_for(theme.home_top.banner.link)

each item, index in top_group_list.slice(0, 6)
.recent-post-item(onclick=`pjax.loadUrl("${url_for(item.path)}")`)
.post_cover.left_radius
a(href=item.path, title=item.title)
span.recent-post-top-text 荐
img.post_bg(onerror=`this.onerror=null;this.src='${url_for(theme.error_img.post_page)}'`, alt='cover', src=item.cover)
.recent-post-info
a.article-title(href=item.path, title=item.title)=item.title
a#todayCard.todayCard(href=top_group_banner_link, style='z-index: 1;')
.todayCard-info
.todayCard-tips=top_group_banner_tips
.todayCard-title=top_group_banner_title
img.todayCard-cover(src=url_for(top_group_banner_image) onerror=`this.onerror=null;this.src='${url_for(theme.error_img.post_page)}'` alt='封面')
.banner-button-group
.banner-button(onclick='event.stopPropagation();event.preventDefault();anzhiyu.hideTodayCard();')
i.anzhiyufont.anzhiyu-icon-arrow-circle-right
span.banner-button-text 更多推荐

link(rel="stylesheet", href=theme.home_top.banner.top_group_banner_css)

if theme.home_top.swiper.enable
if !site.data.essay
script(src=url_for(theme.home_top.swiper.swiper_js))
script.
function initBlogSlider() {
var swiper = new Swiper(".blog-slider", {
passiveListeners: true,
spaceBetween: 30,
effect: "fade",
loop: true,
autoplay: {
disableOnInteraction: true,
delay: 3000,
},
mousewheel: {
passive: true, // 将 mousewheel 事件处理程序标记为被动的
},
// autoHeight: true,
pagination: {
el: ".blog-slider__pagination",
clickable: true,
},
});

var comtainer = document.getElementById("swiper_container");
if (comtainer !== null) {
comtainer.onmouseenter = function () {
swiper.autoplay.stop();
};
comtainer.onmouseleave = function () {
swiper.autoplay.start();
};
}
}
setTimeout(()=>{
initBlogSlider()
}, 100)

上面的代码需要重点关注

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
if theme.home_top.swiper.enable
#swiper_container_blog
#swiper_container.blog-slider.swiper-container-fade.swiper-container-horizontal
.blog-slider__wrp.swiper-wrapper(style='transition-duration: 0ms;')
//- 文章卡片
- let swiper_list = sort_attr_post("swiper_list")
each item in swiper_list
.blog-slider__item.swiper-slide(style=`width: 100%; height: 100%; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms; background: ${getRandomGradient()};`)
a.blog-slider__img(href=item.path, title=item.title)
img(width='48' height='48' src=item.cover alt='图片' onerror=`this.src=` + error_img + `; this.onerror = null;`)
.blog-slider__content
a.blog-slider__title(href=item.path, alt=item.title)= item.title
if (timemode === 'updated' )
span.blog-slider__code= item.updated.format('YYYY-MM-DD')
else
span.blog-slider__code= item.date.format('YYYY-MM-DD')
- const content = strip_html(item.content)
- let expert = content.substring(0, theme.index_post_content.length)
- content.length > theme.index_post_content.length ? expert += ' ...' : ''
.blog-slider__text= truncateText(item.description ? item.description : expert, 80)
a.blog-slider__button(href=item.path, alt=item.title) 详情
//- 轮播图组件
.blog-slider__pagination.swiper-pagination-clickable.swiper-pagination-bullets

我调整的调用顺序,是 标题时间文章简介,大家可以比对代码自己调整修改。

4.当前问题

4.1.基础性改变

仅仅是修改了 分页器 的位置,调整了 标题时间文章简介 调用顺序。

4.2.缺失弥补

没有添加幻灯片特有的 左右切换箭头

4.3.用旧舍新

调用的 swiper 文件都比较老旧,尝试着使用新版本 swiperCSSJS 文件都不成功,水平太菜,本着能跑就用的原则,这一点儿需要注意。

4.4.幻灯片调整的新增功能

主要是为了衬托 swiper 分页器的小圆点,不然显示不出来。

4.4.1.新增了背景颜色渐变

大家注意 pug 文件顶部的代码,如下

1
- const getRandomGradient = () => `linear-gradient(${Math.floor(Math.random()*360)}deg, rgb(${Math.floor(Math.random()*255)},${Math.floor(Math.random()*255)},${Math.floor(Math.random()*255)}), rgb(${Math.floor(Math.random()*255)},${Math.floor(Math.random()*255)},${Math.floor(Math.random()*255)}))`

代码调用为

1
.blog-slider__item.swiper-slide(style=`width: 100%; height: 100%; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms; background: ${getRandomGradient()};`)
4.4.2.增加了内容简介字数截取

大家注意 pug 文件顶部的代码,如下

1
- const truncateText = (text, maxLength) => !text ? '' : (text.length > maxLength ? text.substring(0, maxLength) + ' ...' : text)

幻灯片调用中,对内容简介的调用代码

1
.blog-slider__text= truncateText(item.description ? item.description : expert, 80)

在这里输入调用的内容简介的字数;

4.4.3.幻灯片调用缩略图当背景图片

无论是采用渐变背景,还是当前文章的缩略图当背景,都是为了衬托 swiper 分页器的小圆点,否则小圆点不突出,当前是 蓝白 搭配。

1
.blog-slider__item.swiper-slide(style=`width: 100%; height: 100%; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms; ${item.cover ? `background: url('${item.cover}');` : ''}`)

如果仅仅是拿文章的缩略图当做背景图片,不需要显示额外图片,只需要把

1
2
a.blog-slider__img(href=item.path, title=item.title)
img(width='48' height='48' src=item.cover alt='图片' onerror=`this.src=` + error_img + `; this.onerror = null;`)

中的 src=item.cover 删除掉就可以,不过我没有测试。后续更新仍然在努力中。

希望技术好的前端帮我解决一个问题,就是如何整合新的 swiper 文件,我测试的时候,如果使用新的 swiper 文件,即刻说说乱套。

5.最终更新版

5.1.修改配置文件

本次修改主要是为了调用新版的 swiper 文件,我们这里引入了 11.0.5

1
2
3
4
swiper:
enable: true
swiper_css: https://cdn.bootcdn.net/ajax/libs/Swiper/11.0.5/swiper-bundle.min.css #swiper css依赖
swiper_js: https://cdn.bootcdn.net/ajax/libs/Swiper/11.0.5/swiper-bundle.min.js #swiper js依赖

5.2.修改主题模板文件

/themes/anzhiyu/layout/includes/top/ 文件夹中,修改 top.pug 文件,用下面的代码替换

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
- let home_top_config = theme.home_top
- let { title, subTitle, siteText,default_descr } = home_top_config
if home_top_config.enable
#home_top
.swiper_container_card(style='height: auto;width: 100%')
#bannerGroup
#random-banner
if theme.peoplecanvas.enable
canvas#peoplecanvas
else
.banners-title
.banners-title-big=title
.banners-title-big=subTitle
.banners-title-small=siteText
include ../anzhiyu/tags-group-all.pug

a#random-hover(href='javascript:toRandomPost()')
i.anzhiyufont.anzhiyu-icon-paper-plane
.bannerText 随便逛逛
i.anzhiyufont.anzhiyu-icon-arrow-right
.categoryGroup
each item in home_top_config.category
.categoryItem(style=`box-shadow:${item.shadow}`)
a.categoryButton(href=url_for(item.path) class=`${item.class}`)
span.categoryButtonText=item.name
if (item.icon.startsWith("fa"))
i(class=`${item.icon}`)
else if (item.icon.startsWith("icon"))
svg.icon(aria-hidden="true")
use(xlink:href=`#`+ item.icon)
else if (item.icon.startsWith("anzhiyu"))
i.anzhiyufont(class=`${item.icon}`)
if theme.home_top.swiper.enable
#swiper_container_blog
#swiperBox
#swiper_container.blog-slider.swiper
.swiper-wrapper
- let swiper_list = sort_attr_post("swiper_list")
each item in swiper_list
.blog-slider__item.swiper-slide(style=`background: url("${item.cover || error_img}");`)
.blog-slider__content
a.blog-slider__title(href=item.path, title=item.title)= item.title
if (timemode === 'updated' )
span.blog-slider__code= item.updated.format('YYYY-MM-DD')
else
span.blog-slider__code= item.date.format('YYYY-MM-DD')
- const content = strip_html(item.content)
- let expert = content.substring(0, theme.index_post_content.length)
- content.length > theme.index_post_content.length ? expert += ' ...' : ''
.blog-slider__text= item.description ? item.description : expert
.swiper-button-next
.swiper-button-prev
.blog-slider__pagination.swiper-pagination

#topPostGroup
- let top_group_list = sort_attr_post("top_group_list").splice(0, 4)
each item in top_group_list
.top-group-list-item
.post_cover.left_radius
a(href=item.path title=item.title)
span.top-group-text 荐
img.post_bg(alt=item.title, src=item.cover, onerror=`this.src=` + error_img + `; this.onerror = null;`)
.top-group-info
a.article-title(href=item.path title=item.title)=item.title
.top-group-list-none
.top-group-list-none
.top-group-list-none
else
.topGroup
- let top_group_list = sort_attr_post("top_group_list").splice(0, 6)
- let top_group_banner_tips = theme.home_top.banner.tips
- let top_group_banner_title = theme.home_top.banner.title
- let top_group_banner_image = theme.home_top.banner.image
- let top_group_banner_link = url_for(theme.home_top.banner.link)

each item, index in top_group_list.slice(0, 6)
.recent-post-item(onclick=`pjax.loadUrl("${url_for(item.path)}")`)
.post_cover.left_radius
a(href=item.path, title=item.title)
span.recent-post-top-text 荐
img.post_bg(onerror=`this.onerror=null;this.src='${url_for(theme.error_img.post_page)}'`, alt='cover', src=item.cover)
.recent-post-info
a.article-title(href=item.path, title=item.title)=item.title
a#todayCard.todayCard(href=top_group_banner_link, style='z-index: 1;')
.todayCard-info
.todayCard-tips=top_group_banner_tips
.todayCard-title=top_group_banner_title
img.todayCard-cover(src=url_for(top_group_banner_image) onerror=`this.onerror=null;this.src='${url_for(theme.error_img.post_page)}'` alt='封面')
.banner-button-group
.banner-button(onclick='event.stopPropagation();event.preventDefault();anzhiyu.hideTodayCard();')
i.anzhiyufont.anzhiyu-icon-arrow-circle-right
span.banner-button-text 更多推荐

link(rel="stylesheet", href=theme.home_top.banner.top_group_banner_css)

if theme.home_top.swiper.enable
if !site.data.essay
script(src=url_for(theme.home_top.swiper.swiper_js))
script.
function initBlogSlider() {
var swiper = new Swiper(".blog-slider", {
passiveListeners: true,
spaceBetween: 30,
effect: "fade",
loop: true,
autoplay: {
disableOnInteraction: true,
delay: 3000,
},
mousewheel: {
passive: true, // 将 mousewheel 事件处理程序标记为被动的
},
// autoHeight: true,
pagination: {
el: ".blog-slider__pagination",
clickable: true,
},
// 新增:上下页切换按钮配置(仅新增这一段)
navigation: {
nextEl: ".swiper-button-next",
prevEl: ".swiper-button-prev",
},
});

var comtainer = document.getElementById("swiper_container");
if (comtainer !== null) {
comtainer.onmouseenter = function () {
swiper.autoplay.stop();
};
comtainer.onmouseleave = function () {
swiper.autoplay.start();
};
}
}
setTimeout(()=>{
initBlogSlider()
}, 100)

主要是做了两个修改,其一是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
if theme.home_top.swiper.enable
#swiper_container_blog
#swiperBox
#swiper_container.blog-slider.swiper
.swiper-wrapper
- let swiper_list = sort_attr_post("swiper_list")
each item in swiper_list
.blog-slider__item.swiper-slide(style=`background: url("${item.cover || error_img}");`)
.blog-slider__content
a.blog-slider__title(href=item.path, title=item.title)= item.title
if (timemode === 'updated' )
span.blog-slider__code= item.updated.format('YYYY-MM-DD')
else
span.blog-slider__code= item.date.format('YYYY-MM-DD')
- const content = strip_html(item.content)
- let expert = content.substring(0, theme.index_post_content.length)
- content.length > theme.index_post_content.length ? expert += ' ...' : ''
.blog-slider__text= item.description ? item.description : expert
.swiper-button-next
.swiper-button-prev
.blog-slider__pagination.swiper-pagination

修改了幻灯片代码,添加了左右切换按钮,其二是该文件下面的 JavaScript 代码,添加了左右切换代码的调用

1
2
3
4
5
// 新增:上下页切换按钮配置(仅新增这一段)
navigation: {
nextEl: ".swiper-button-next",
prevEl: ".swiper-button-prev",
},

5.3.修改css样式

/themes/anzhiyu/source/css_extra/home_top/ 文件夹中,找到 swiperstyle.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
.swiper_container_card {
display: flex !important;
justify-content: space-around;
flex-direction: row !important;
user-select: none;
}

#swiper_container_blog {
width: 100%;
transition: all 0.3s, width 0s;
border-radius: 12px;
}
.swiper-wrapper {
align-items: center;
}

div#swiper_container {
transition: 0.3s;
}
/* 防止轮播图css未加载完时的超度横行滚动条 */
#swiper_container {
overflow: hidden !important;
}
div.swiper_container_card {
margin: 1rem auto 0;
}

#swiperBox {
width: 100% !important;
height: 202px !important;
padding: 0 !important;
margin: 0 auto !important;
position: relative !important;
overflow: hidden !important;
border: none !important;
border-radius: 12px;
}

#swiper_container {
width: 100% !important;
height: 100% !important;
opacity: 1 !important;
position: relative !important;
transition: all .3s ease !important;
z-index: 1 !important;
}

.blog-slider {
width: 100% !important;
height: 100% !important;
position: relative !important;
}

.blog-slider__item {
width: 100% !important;
height: 100% !important;
background-size: cover !important;
background-position: center !important;
position: relative !important;
}

.blog-slider__item::after {
content: '' !important;
position: absolute !important;
width: 100% !important;
height: 100% !important;
background-color: rgba(0, 0, 0, .1) !important;
z-index: -1 !important;
left: 0 !important;
top: 0 !important;
}

.blog-slider__content {
width: 100% !important;
height: 100% !important;
padding: 0 50px 20px !important;
display: flex !important;
justify-content: center !important;
align-items: center !important;
flex-direction: column !important;
margin: 0 !important;
}

.blog-slider__title {
font-size: 2rem !important;
color: #fff !important;
text-align: center !important;
line-height: 1.5 !important;
margin: 2px 0 !important;
transition: all .3s !important;
text-decoration: none !important;
font-weight: 700;
margin-bottom: 15px;
-webkit-line-clamp: 1;
display: -webkit-box;
overflow: hidden;
-webkit-box-orient: vertical;
}

.blog-slider__code {
color: var(--font-color);
margin-bottom: 0;
display: block;
font-weight: 500;
}

.blog-slider__text {
color: var(--font-color);
margin-bottom: 15px;
line-height: 1.5em;
width: 100%;
height: 50px;
word-break: break-all;
word-wrap: break-word;
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
}

.blog-slider__code, .blog-slider__text {
color: #fff !important;
text-align: center !important;
line-height: 1.5 !important;
margin: 2px 0 !important;
}

.blog-slider__pagination {
position: absolute !important;
bottom: 15px !important;
left: 50% !important;
transform: translateX(-50%) !important;
z-index: 21 !important;
text-align: center !important;
}

.swiper-pagination-bullet {
margin: 0 8px !important;
width: 11px !important;
height: 11px !important;
border-radius: 50% !important;
background: #fff !important;
opacity: .8 !important;
}

.swiper-pagination-bullet-active {
opacity: 1 !important;
background: #4976f5 !important;
width: 30px !important;
border-radius: 10px !important;
}

.swiper-button-next, .swiper-button-prev {
width: 40px !important;
height: 40px !important;
top: 50% !important;
transform: translateY(-50%) !important;
z-index: 20 !important;
background: rgba(255,255,255,0.2) !important;
border-radius: 50% !important;
}

.swiper-button-next {
right: 10px !important;
}

.swiper-button-prev {
left: 10px !important;
}

.swiper-button-next,
.swiper-button-prev {
width: 36px !important;
height: 36px !important;
top: 65% !important;
transform: translateY(-50%) !important;
z-index: 20 !important;
background: rgba(255, 255, 255, 0.25) !important;
border-radius: 50% !important;
backdrop-filter: blur(8px) !important;
border: 1px solid rgba(255, 255, 255, 0.4) !important;
cursor: pointer !important;
transition: all 0.3s ease !important;
font-size: 0 !important;
outline: none !important;
}

.swiper-button-next::after {
content: '' !important;
position: absolute !important;
width: 14px !important;
height: 14px !important;
border-top: 2px solid #fff !important;
border-right: 2px solid #fff !important;
top: 50% !important;
left: 40% !important;
transform: translate(-45%, -55%) rotate(45deg) !important;
}

.swiper-button-prev::after {
content: '' !important;
position: absolute !important;
width: 14px !important;
height: 14px !important;
border-top: 2px solid #fff !important;
border-right: 2px solid #fff !important;
top: 50% !important;
left: 60% !important;
transform: translate(-55%, -45%) rotate(-135deg) !important;
}

.swiper-button-next {
right: 20px !important;
}

.swiper-button-prev {
left: 20px !important;
}

.swiper-button-next:hover,
.swiper-button-prev:hover {
background: rgba(73, 118, 245, 0.8) !important;
border-color: rgba(255, 255, 255, 0.8) !important;
transform: translateY(-50%) scale(1.08) !important;
}

.swiper-button-next:active,
.swiper-button-prev:active {
transform: translateY(-50%) scale(0.95) !important;
background: rgba(73, 118, 245, 0.9) !important;
}

/* 合并后的手机端样式(max-width:768px):整合原有两个768px查询的所有样式 */
@media screen and (max-width: 768px) {
/* 按钮样式 */
.swiper-button-next,
.swiper-button-prev {
width: 40px !important;
height: 40px !important;
}

.swiper-button-next {
right: 8px !important;
}

.swiper-button-prev {
left: 8px !important;
}

.swiper-button-next::after,
.swiper-button-prev::after {
width: 10px !important;
height: 10px !important;
}

/* 轮播容器/内容样式 */
#swiper_container_blog {
width: 100%;
transition: all 0.3s, width 0s;
border-radius: 12px;
}

#swiperBox {
height: 200px !important;
}

.blog-slider__title {
font-size: 1.3rem !important;
}

.blog-slider__content {
padding: 0 20px !important;
}
}

@media screen and (min-width: 768px) and (max-width: 1024px) {
#swiper_container_blog {
width: 100%;
transition: all 0.3s, width 0s;
border-radius: 12px;
}
}

这段代码有小问题,

1
2
3
4
5
#swiper_container_blog {
width: 100%;
transition: all 0.3s, width 0s;
border-radius: 12px;
}

有重复判断的嫌疑,时间关系来不及调整。

5.4.即可短文的问题

使用新的 swiper 文件,首页即刻短文调用会有问题,至少我这里遇到,解决方案用 CSS 样式补丁 。

/themes/anzhiyu/source/css_extra/essay_page/ 文件夹中,找到 home_essay_bar.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
/* 核心容器样式 - 限制高度+隐藏溢出 */
.essay_bar_swiper_container {
height: 30px !important; /* 与单个slide高度一致 */
overflow: hidden !important;
position: relative !important;
}

/* 单个slide样式 - 强制独占高度,避免换行/重叠 */
.essay_bar_swiper_container .li-style {
display: block !important;
height: 30px !important; /* 固定高度,与容器一致 */
line-height: 30px !important; /* 垂直居中 */
overflow: hidden !important;
text-overflow: ellipsis !important;
white-space: nowrap !important;
width: 100% !important;
}

/* Swiper wrapper 样式 - 新版必须强制定位 */
.essay_bar_swiper_container .swiper-wrapper {
position: absolute !important;
top: 0 !important;
left: 0 !important;
width: 100% !important;
}

如果还有问题,需要调用 JavaScript 代码,不过也许没有必要:

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
// jike.js - 即刻短文轮播功能(合并优化版)
(function() {
// 等待DOM完全加载后执行,避免元素未加载导致初始化失败
document.addEventListener('DOMContentLoaded', function() {
// 获取Swiper容器
const swiperContainer = document.getElementById('bbtalk');

// 容错处理:容器不存在则终止执行
if (!swiperContainer) {
console.warn('未找到轮播容器 #bbtalk,轮播初始化失败');
return;
}

// 移除禁用滑动的类(解决新版Swiper轮播失效的关键)
swiperContainer.classList.remove('swiper-no-swiping');

// 初始化Swiper(整合两段代码的所有有效配置)
const jikeSwiper = new Swiper(swiperContainer, {
// 核心布局配置
direction: 'vertical', // 垂直轮播
slidesPerView: 1, // 仅显示1个slide
spaceBetween: 0, // 取消slide间距
loop: true, // 循环轮播
speed: 300, // 切换动画速度

// 自动轮播配置(整合两段代码的autoplay)
autoplay: {
delay: 3000, // 3秒切换一次
disableOnInteraction: false, // 点击后仍自动轮播
pauseOnMouseEnter: true, // 鼠标悬停暂停(新增的人性化配置)
resumeOnMouseLeave: true // 鼠标离开恢复轮播
},

// 交互配置
allowTouchMove: false, // 禁用手动滑动

// 兼容性配置(解决动态DOM变化导致的轮播异常)
observer: true,
observeParents: true
});

// 兜底启动轮播(确保autoplay100%生效)
setTimeout(() => {
jikeSwiper.autoplay.start();
}, 500);

// 暴露实例到全局(方便控制台调试)
window.jikeSwiper = jikeSwiper;
});
})();

但愿你别遇到这些乱七八糟的问题。

5.结束语

最后 Hexo 三板斧,就可以在页面侧边栏查看效果。欢迎大家在评论区给予指正,让我们共同打造快节奏时代,不受算法牵制的属于我们自己的小空间!