I am creating a photo carousel using swiperJS ref link here.
Here are the params of the swiper:
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'
});
My purpose is to fit as many slides into the view as possible. The photos are of different width and size, some are landscape mode and some are portrait.
Problem: only showing one slide per view.
Goal: fit in as many slides as possible into view.
could somebody help me to achieve this?
Swiper slides are set by default to 100% width in swiper.css:
.swiper-slide {
...
width: 100%;
height: 100%;
...
}
When you initialize swiper with slidesPerView: 'auto'
, it will not explicitly set the width sizes of the slides to a calculated number (like it would if you define the slides per view to a fixed number) but it will use the existing slide widths. Without any css/inline style changes, these widths will be 100%.
Note that in the example on the swiper website for slidesPerView: 'auto'
you can see how they explicitly set the widths of the slides they want to have a different width in css:
.swiper-slide:nth-child(2n) {
width: 40%;
}
.swiper-slide:nth-child(3n) {
width: 20%;
}
So if you want to use the original image widths just set the swiper slide widths to auto:
.swiper-slide {
width: auto;
}
Code snippet to show how it works:
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With