我正在使用swiperJS ref link here制作照片轮播。
以下是狙击手的参数:
mySwiper = Swiper('.swiper-container', {
loop: false,
speed: 800,
slidesPerView: 'auto',
grabCursor: true,
spaceBetween: 2,
initialSlide: 0,
keyboardControl: false,
resizeReInit: true,
autoplayDisableOnInteraction: false,
pagination: '.swiper-pagination',
paginationClickable: false,
nextButton: '.swiper-next',
prevButton: '.swiper-prev'
});
我的目的是尽可能多地将幻灯片放入视图中。照片有不同的宽度和大小,有些是横向模式,有些是肖像。
问题:每个视图只显示一张幻灯片。
目标:尽可能多地放入视图中。
有人可以帮助我实现这个目标吗?
答案 0 :(得分:1)
在swiper.css中,滑行幻灯片默认设置为100%宽度:
.swiper-slide {
...
width: 100%;
height: 100%;
...
}
当您使用slidesPerView: 'auto'
初始化swiper时,它不会将幻灯片的宽度大小显式设置为计算得出的数字(就像您将每个视图的幻灯片定义为固定数一样),但是它将使用现有幻灯片宽度。如果没有任何CSS /内联样式更改,则这些宽度将为100%。
请注意,在slidesPerView: 'auto'
的滑动器网站上的example中,您可以看到他们如何显式设置他们想要在CSS中具有不同宽度的幻灯片的宽度:
.swiper-slide:nth-child(2n) {
width: 40%;
}
.swiper-slide:nth-child(3n) {
width: 20%;
}
因此,如果要使用原始图像宽度,只需将滑动滑杆的宽度设置为自动:
.swiper-slide {
width: auto;
}
代码片段以显示其工作方式:
var swiper = new Swiper('.swiper-container', {
slidesPerView: 'auto',
centeredSlides: true,
spaceBetween: 30,
pagination: {
el: '.swiper-pagination',
clickable: true,
},
});
html, body {
position: relative;
height: 100%;
}
body {
background: #eee;
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
font-size: 14px;
color:#000;
margin: 0;
padding: 0;
}
.swiper-container {
width: 100%;
height: 100%;
}
.swiper-slide {
text-align: center;
font-size: 18px;
background: #fff;
/* Center slide text vertically */
display: -webkit-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
-webkit-box-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
}
/* this is the important part */
.swiper-container .swiper-slide {
width: auto;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Swiper demo</title>
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.3.5/css/swiper.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.3.5/js/swiper.min.js"></script>
</head>
<body>
<!-- Swiper -->
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide"><img src="https://via.placeholder.com/150x150"></div>
<div class="swiper-slide"><img src="https://via.placeholder.com/350x150"></div>
<div class="swiper-slide"><img src="https://via.placeholder.com/250x150"></div>
<div class="swiper-slide"><img src="https://via.placeholder.com/450x150"></div>
<div class="swiper-slide"><img src="https://via.placeholder.com/250x150"></div>
<div class="swiper-slide"><img src="https://via.placeholder.com/550x150"></div>
<div class="swiper-slide"><img src="https://via.placeholder.com/300x150"></div>
<div class="swiper-slide"><img src="https://via.placeholder.com/400x150"></div>
<div class="swiper-slide"><img src="https://via.placeholder.com/500x150"></div>
<div class="swiper-slide"><img src="https://via.placeholder.com/200x150"></div>
<div class="swiper-slide">how about</div>
<div class="swiper-slide">fitting some text</div>
<div class="swiper-slide">also</div>
</div>
<!-- Add Pagination -->
<div class="swiper-pagination"></div>
</div>
<!-- Initialize Swiper -->
<script>
</script>
</body>
</html>