给Hexo Next主题添加首页轮播图

本博客首页设置了轮播图,大家可以去看一下效果。

 下面教大家如何给自己的的博客添加首页轮播图!

 在themes\next\layout\_macro目录下新建carousel.swig文件,里面粘贴入提下代码:

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
{% if theme.carousel.enable %}
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<meta name="referrer" content="no-referrer" />
<style type="text/css">
.glyphicon-chevron-left:before{
content: ""
}
.glyphicon-chevron-right:before{
content: ""
}

.right-siders{
border-radius: 10px;
margin-bottom: 5px;
}

.my-carousel:hover{
margin-left: 5px;
border-radius: 5px;
transform: scale(1.1);
box-shadow: 10px 10px 15px 2px rgba(0,0,0,.12), 0 0 6px 0 rgba(104, 104, 105, 0.1);
}

.carousel{
border-radius: 20px;
width: 100%;
height: 280px;
position: relative;
margin-top: 20px;
}

@media all and (max-width: 768px){
.carousel{
border-radius: 20px;
width: 100% !important;
height: 103px !important;
position: relative;
}
}

.carousel-inner {
position: relative;
overflow: hidden;
width: 100%;
height: 100%
}
.carousel-inner > .item {
display: none;
position: relative;
-webkit-transition: 0.6s ease-in-out left;
-o-transition: 0.6s ease-in-out left;
transition: 0.6s ease-in-out left;
}
.carousel-inner > .item > img,
.carousel-inner > .item > a > img {
line-height: 1;
}
@media all and (transform-3d), (-webkit-transform-3d) {
.carousel-inner > .item {
-webkit-transition: -webkit-transform 0.6s ease-in-out;
-moz-transition: -moz-transform 0.6s ease-in-out;
-o-transition: -o-transform 0.6s ease-in-out;
transition: transform 0.6s ease-in-out;
-webkit-backface-visibility: hidden;
-moz-backface-visibility: hidden;
backface-visibility: hidden;
-webkit-perspective: 1000px;
-moz-perspective: 1000px;
perspective: 1000px;
}
.carousel-inner > .item.next,
.carousel-inner > .item.active.right {
-webkit-transform: translate3d(100%, 0, 0);
transform: translate3d(100%, 0, 0);
left: 0;
}
.carousel-inner > .item.prev,
.carousel-inner > .item.active.left {
-webkit-transform: translate3d(-100%, 0, 0);
transform: translate3d(-100%, 0, 0);
left: 0;
}
.carousel-inner > .item.next.left,
.carousel-inner > .item.prev.right,
.carousel-inner > .item.active {
-webkit-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
left: 0;
}
}
.carousel-inner > .active,
.carousel-inner > .next,
.carousel-inner > .prev {
display: block;
}
.carousel-inner > .active {
left: 0;
}
.carousel-inner > .next,
.carousel-inner > .prev {
position: absolute;
top: 0;
width: 100%;
}
.carousel-inner > .next {
left: 100%;
}
.carousel-inner > .prev {
left: -100%;
}
.carousel-inner > .next.left,
.carousel-inner > .prev.right {
left: 0;
}
.carousel-inner > .active.left {
left: -100%;
}
.carousel-inner > .active.right {
left: 100%;
}
.carousel-control {
position: absolute;
top: 0;
left: 0;
bottom: 0;
opacity: 0.5;
filter: alpha(opacity=50);
font-size: 20px;
color: #fff;
text-align: center;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.6);
background-color: rgba(0, 0, 0, 0);
}
.carousel-control.left {
background-image: -webkit-linear-gradient(left, rgba(0, 0, 0, 0.5) 0%, rgba(0, 0, 0, 0.0001) 100%);
background-image: -o-linear-gradient(left, rgba(0, 0, 0, 0.5) 0%, rgba(0, 0, 0, 0.0001) 100%);
background-image: linear-gradient(to right, rgba(0, 0, 0, 0.5) 0%, rgba(0, 0, 0, 0.0001) 100%);
background-repeat: repeat-x;
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#80000000, endColorstr=#00000000, GradientType=1);
}
.carousel-control.right {
left: auto;
right: 0;
background-image: -webkit-linear-gradient(left, rgba(0, 0, 0, 0.0001) 0%, rgba(0, 0, 0, 0.5) 100%);
background-image: -o-linear-gradient(left, rgba(0, 0, 0, 0.0001) 0%, rgba(0, 0, 0, 0.5) 100%);
background-image: linear-gradient(to right, rgba(0, 0, 0, 0.0001) 0%, rgba(0, 0, 0, 0.5) 100%);
background-repeat: repeat-x;
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#00000000, endColorstr=#80000000, GradientType=1);
}
.carousel-control:hover,
.carousel-control:focus {
outline: 0;
color: #fff;
text-decoration: none;
opacity: 0.9;
filter: alpha(opacity=90);
}
.carousel-control .icon-prev,
.carousel-control .icon-next,
.carousel-control .glyphicon-chevron-left,
.carousel-control .glyphicon-chevron-right {
position: absolute;
top: 50%;
margin-top: -10px;
z-index: 5;
display: inline-block;
}

.carousel-control .icon-prev,
.carousel-control .glyphicon-chevron-left {
background-position-y: -20px;
left: 9px;
}

@media all and (max-width: 768px){
.carousel-control .icon-prev,
.carousel-control .glyphicon-chevron-left {
background-position-y: -20px;
left: 0px;
}
}

.carousel-control .icon-next,
.carousel-control .glyphicon-chevron-right {
/*background: url('/images/icon-slides.png');*/
background-position-x: -50px;
background-position-y: -20px;
right: 0px;
}

.carousel-control .icon-prev,
.carousel-control .icon-next {
width: 20px;
height: 20px;
line-height: 1;
font-family: serif;
}

.carousel-control .icon-prev:before {
content: 2039;
}
.carousel-control .icon-next:before {
content: 203a;
}

.carousel-indicators {
position: absolute;
bottom: -25px;
left: 50%;
z-index: 15;
width: 60%;
margin-left: -30%;
padding-left: 0;
list-style: none;
text-align: center;
}
.carousel-indicators li {
display: inline-block;
width: 30px;
height: 3px;
margin: 5px;
text-indent: -999px;
border: 1px solid #bbb;
border-radius: 10px;
cursor: pointer;
background-color: rgba(0, 0, 0, .24);
}
.carousel-indicators .active {
width: 30px;
height: 3px;
background-color: #fff;
}
.carousel-caption {
position: absolute;
left: 15%;
right: 15%;
bottom: 20px;
z-index: 10;
padding-top: 20px;
padding-bottom: 20px;
color: #fff;
text-align: center;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.6);
}
.carousel-caption .btn {
text-shadow: none;
}
@media screen and (min-width: 768px) {
.carousel-control .glyphicon-chevron-left,
.carousel-control .glyphicon-chevron-right,
.carousel-control .icon-prev,
.carousel-control .icon-next {
width: 30px;
height: 30px;
margin-top: -10px;
font-size: 30px;
}
.carousel-control .glyphicon-chevron-left,
.carousel-control .icon-prev {
margin-left: -10px;
}
.carousel-control .glyphicon-chevron-right,
.carousel-control .icon-next {
}
.carousel-caption {
left: 20%;
right: 20%;
padding-bottom: 30px;
}
.carousel-indicators {
bottom: -10px;
}
}
@media all and (max-width: 768px){
.carousel-indicators li {
display: inline-block;
width: 20px !important;
height: 3px;
margin: 2.1px !important;
text-indent: -999px;
border: 1px solid #bbb;
border-radius: 10px;
cursor: pointer;
background-color: rgba(0, 0, 0, .24);
}
.carousel-indicators .active {
width: 20px !important;
height: 3px;
background-color: #fff;
}
}
</style>
<div id="myCarousel" class="carousel slide" height="100%" data-ride="carousel" data-interval="3500">
<!-- 轮播(Carousel)指标 -->
<ol class="carousel-indicators">
{% set index = 0 %}
{% for item in theme.carousel.item %}
<li data-target="#myCarousel" data-slide-to="{{index}}"></li>
{% set index = index+1 %}
{% endfor %}
</ol>
<!-- 轮播(Carousel)项目 -->
<div class="carousel-inner" style="border: 0px; border-radius: 20px; width: 100%;">
{% set act = 0 %}
{% for item in theme.carousel.item %}
{% if act===0 %}
<a class="item active" href="{{ url_for(item.link) }}" target="_blank" style="height: 100%;">
{# 添加 class="nofancybox" 解决fancybox和图片链接冲突 #}
<img src="{{item.img}}" class="nofancybox" style="width: 100%; height: 100%" >
</a>
{% set act = 1 %}
{% elseif act===1 %}
<a class="item" href="{{ url_for(item.link) }}" target="_blank" style="height: 100%;">
<img src="{{item.img}}" class="nofancybox" style="width: 100%; height: 100%;" >
</a>
{% endif %}
{% endfor %}
</div>
<!-- 轮播(Carousel)导航 -->
<a class="left carousel-control" data-target="#myCarousel" href="javascript:void(0);" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
</a>
<a class="right carousel-control" data-target="#myCarousel" href="javascript:void(0);" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
</a>
</div>
{% endif %}

 在themes\next\layout\index.swig中找到{% block content %},在其下方加上如下代码(引入新建的carousel.swig文件):

1
{% include '_macro/carousel.swig' %}

 在主题配置文件的末尾加上如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
# 配置首页轮播图
carousel:
enable: true
item: [
{
'link':'文章链接地址',
'img':'图片链接地址'
},
{
'link':'文章链接地址',
'img':'图片链接地址'
},
]

完成以上配置后,轮播图就已经实现了,但是可能还会出现以下问题,比如有些用户开着FancyBox功能,这将会导致点击轮播图片的时候出现如下错误:

链接无法跳转

 为了解决这个问题,可以在themes\next\source\js\utils.js的如下位置加上第八行代码(next版本不同,utils.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
/**
* Wrap images with fancybox.
*/
wrapImageWithFancyBox: function() {
document.querySelectorAll('.post-body :not(a) > img, .post-body > img').forEach(element => {
var $image = $(element);
/*解决fancybox和图片链接冲突*/
+ if ($(this).hasClass('nofancybox')) return;

var imageLink = $image.attr('data-src') || $image.attr('src');
var $imageWrapLink = $image.wrap(`<a class="fancybox fancybox.image" href="${imageLink}" itemscope itemtype="http://schema.org/ImageObject" itemprop="url"></a>`).parent('a');
if ($image.is('.post-gallery img')) {
$imageWrapLink.attr('data-fancybox', 'gallery').attr('rel', 'gallery');
} else if ($image.is('.group-picture img')) {
$imageWrapLink.attr('data-fancybox', 'group').attr('rel', 'group');
} else {
$imageWrapLink.attr('data-fancybox', 'default').attr('rel', 'default');
}

var imageTitle = $image.attr('title') || $image.attr('alt');
if (imageTitle) {
$imageWrapLink.append(`<p class="image-caption">${imageTitle}</p>`);
// Make sure img title tag will show correctly in fancybox
$imageWrapLink.attr('title', imageTitle).attr('data-caption', imageTitle);
}
});

$.fancybox.defaults.hash = false;
$('.fancybox').fancybox({
loop : true,
helpers: {
overlay: {
locked: false
}
}
});
},

 至此,轮播图已经创建完成了,轮播图的长宽比是按照我自己的喜好设置的,大家可以根据自己的喜好修改carousel.swig里的内容,从而达到自己想要的效果。

 以下是我写的,在手机端做了适配,避免了手机端显示的图片比例失调的问题:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//默认的参数
.carousel{
border-radius: 20px;
width: 100%;
height: 280px; //图片高度
position: relative;
margin-top: 20px;
}

//手机端的参数
@media all and (max-width: 768px){
.carousel{
border-radius: 20px;
width: 100% !important;
height: 103px //图片高度 !important;
position: relative;
}
}

 其实@media all and (max-width: 768px){}的意思就是当屏幕(也许是打字机或盲人阅读器)尺寸小于768px时,应用下面的css样式。也可以这样写@media screen and (min-width:768px) and (max-width:900px) {},意思就是屏幕大于768px,小于900px的设备。大家可自行查阅有关media的内容,这里就不多赘述了😁

 有什么不懂的可以留言或者给我发邮件,我会尽可能地帮助大家解决问题的❤

点击此处查看我的联系方式

------ 本文结束 感谢您的阅读------