Desire output:
I am trying to implement a slider with a fixed height and auto width. The images should be adjusted to e.g. height: 800px and the width should be true to the image resolution.
My Implementation: https://playground-f2831f.webflow.io/model-images/provident-at-sunt-incidunt How it is supposed to be: https://artworld.agency/artists/sarah-bassett
My slider param:
JavaScript
x
56
56
1
<script src="https://cdn.jsdelivr.net/npm/swiper@8/swiper-bundle.min.js"></script>
2
<script>
3
$(".swiper").append(`<div class="swiper-scrollbar"></div>`);
4
$(".swiper").append(`<div class="swiper-pagination"></div>`);
5
$(".swiper").append(`<div class="swiper-arrow button-prev"></div>`);
6
$(".swiper").append(`<div class="swiper-arrow button-next"></div>`);
7
8
const swiper1 = new Swiper(".swiper", {
9
// Optional parameters
10
slidesPerView: 'auto',
11
centeredSlides: false,
12
speed: 1500,
13
loop: false,
14
simulateTouch : true,
15
loopFillGroupWithBlank: false,
16
grabCursor: true,
17
direction: "horizontal",
18
// Responsive breakpoints
19
breakpoints: {
20
// when window width is >= 480px
21
480: {
22
slidesPerView: 1
23
},
24
// when window width is >= 768px
25
768: {
26
slidesPerView: 2
27
},
28
// when window width is >= 992px
29
992: {
30
slidesPerView: 2
31
}
32
},
33
34
// If we need pagination
35
pagination: {
36
el: ".swiper-pagination",
37
clickable: true
38
},
39
40
// Navigation arrows
41
navigation: {
42
nextEl: ".button-next",
43
prevEl: ".button-prev"
44
},
45
46
// And if we need scrollbar
47
scrollbar: {
48
el: ".swiper-scrollbar",
49
draggable: true
50
}
51
});
52
53
54
55
</script>
56
Advertisement
Answer
1 of 2:
Set slidesPerView: "auto"
.
Docs demo: https://swiperjs.com/demos#slides-per-view-auto
In your case the slide width adapt the currently width of the slide content (The <img>
inside). Related: boxmodel
2 of 2:
Set the image height to some value and width to auto.
JavaScript
1
6
1
/* css */
2
.swiper-slide img {
3
width: auto;
4
height: 200px;
5
}
6
*** The width will be proportional to the height
DEMO
JavaScript
1
16
16
1
var swiper = new Swiper(".mySwiper", {
2
slidesPerView: "auto",
3
4
loop: true,
5
6
autoplay: {
7
delay: 5000,
8
},
9
10
slideToClickedSlide: true,
11
spaceBetween: 10,
12
pagination: {
13
el: ".swiper-pagination",
14
clickable: true,
15
},
16
});
JavaScript
1
12
12
1
.swiper-slide img {
2
cursor: pointer;
3
width: auto;
4
height: 200px;
5
border-radius: 10px;
6
object-fit: contain;
7
}
8
9
.gallery-thumbs .swiper-slide {
10
width: auto;
11
border-radius: 10px;
12
}
JavaScript
1
30
30
1
<!-- begin snippet: js hide: false console: true babel: false -->
2
3
<!-- Link Swiper's CSS -->
4
<link rel="stylesheet" href="https://unpkg.com/swiper@8.4.4/swiper-bundle.min.css" />
5
6
7
8
<h1>Swiper 8</h1>
9
<!-- Swiper -->
10
<div class="swiper mySwiper gallery-thumbs">
11
<div class="swiper-wrapper">
12
<div class="swiper-slide">
13
<img src="https://picsum.photos/id/301/1000/900" />
14
</div>
15
<div class="swiper-slide">
16
<img src="https://picsum.photos/id/89/800/1600" />
17
</div>
18
<div class="swiper-slide">
19
<img src="https://picsum.photos/id/24/1500/800" />
20
</div>
21
<div class="swiper-slide">
22
<img src="https://picsum.photos/id/225/1100/500" />
23
</div>
24
</div>
25
<div class="swiper-pagination"></div>
26
27
</div>
28
29
<!-- Swiper JS -->
30
<script src="https://unpkg.com/swiper@8.4.4/swiper-bundle.min.js"></script>