swiper轮播图怎么设置高度一致
时间: 2023-10-25 18:31:37 浏览: 508
要实现 Swiper 轮播图高度一致,需要使用 CSS 设置轮播图容器的高度,并在 Swiper 初始化时设置 `slidesPerView` 属性为 `'auto'`,这样轮播图会自动适应容器宽度,并且高度也会一致。
示例代码:
```html
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide"><img src="slide1.jpg"></div>
<div class="swiper-slide"><img src="slide2.jpg"></div>
<div class="swiper-slide"><img src="slide3.jpg"></div>
</div>
</div>
```
```css
.swiper-container {
height: 300px; /* 设置轮播图容器的高度 */
}
.swiper-slide img {
width: 100%;
height: 100%;
object-fit: cover;
}
```
```js
var mySwiper = new Swiper('.swiper-container', {
slidesPerView: 'auto', // 设置轮播图自动适应容器宽度
loop: true // 开启循环播放
});
```
上面的示例代码中,将轮播图容器的高度设置为 300 像素,并使用 CSS 设置轮播图图片的宽度和高度都为 100%,并使用 `object-fit: cover` 属性让图片填充容器。在 Swiper 初始化时,将 `slidesPerView` 属性设置为 `'auto'`,表示轮播图会自动适应容器宽度,并保持高度一致。同时,开启了循环播放功能。
注意,在使用 Swiper 轮播图时,需要引入 Swiper 的 CSS 和 JS 文件,并按照官方文档进行初始化。
阅读全文
相关推荐

















