Swiper JS 按视图滑动,按列滑动显示不正确
Swiper JS slides per view and slides per column displaying incorrectly
我试图使用 demo given at the official page. However When I copied the code and ran on my browser it displayed like this
使用 swiper js 在 bootstrap 上制作 2 行幻灯片
代码与演示页面完全相同
<!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">
<link rel="stylesheet" href="../package/css/swiper.min.css">
<style>
.swiper-container {
width: 100%;
height: 100%;
margin-left: auto;
margin-right: auto;
}
.swiper-slide {
text-align: center;
font-size: 18px;
background: #fff;
height: calc((100% - 30px) / 2);
/* 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;
}
</style>
</head>
<body>
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</div>
<div class="swiper-slide">Slide 4</div>
<div class="swiper-slide">Slide 5</div>
<div class="swiper-slide">Slide 6</div>
<div class="swiper-slide">Slide 7</div>
<div class="swiper-slide">Slide 8</div>
<div class="swiper-slide">Slide 9</div>
<div class="swiper-slide">Slide 10</div>
</div>
<div class="swiper-pagination"></div>
</div>
<script src="../package/js/swiper.min.js"></script>
<script>
var swiper = new Swiper('.swiper-container', {
slidesPerView: 3,
slidesPerColumn: 2,
spaceBetween: 30,
pagination: {
el: '.swiper-pagination',
clickable: true,
},
});
</script>
</body>
</html>
我尝试更改每个视图的幻灯片和每列的幻灯片,但它们几乎相同。
更改容器高度
.swiper-container { height: 800px; }
我遇到了同样的问题。看来设置slidesPerColumn
不起作用。然后我用这些 Swiper Slide 设置检查哪个 CSS 来自 swiper css:
.swiper-container-multirow-column > .swiper-wrapper {
flex-direction: column;
}
事实证明,当我覆盖 CSS 时,它起作用了:
.swiper-container-multirow-column > .swiper-wrapper {
flex-direction: inherit;
}
或者,添加以下设置:
slidesPerColumnFill: 'row'
希望对您有所帮助(即使是关于此主题的较晚回答)。
如果您使用 CDN ->
<link rel="stylesheet"href="https://unpkg.com/swiper/swiper-bundle.min.css" >
<script src="https://unpkg.com/swiper/swiper-bundle.min.js"></script>
替换为 ->
<link rel="stylesheet"href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.4.6/css/swiper.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.4.6/js/swiper.min.js"></script>
这样就可以了。显然,许多选项不适用于以前的 CDN
我试图使用 demo given at the official page. However When I copied the code and ran on my browser it displayed like this
代码与演示页面完全相同
<!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">
<link rel="stylesheet" href="../package/css/swiper.min.css">
<style>
.swiper-container {
width: 100%;
height: 100%;
margin-left: auto;
margin-right: auto;
}
.swiper-slide {
text-align: center;
font-size: 18px;
background: #fff;
height: calc((100% - 30px) / 2);
/* 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;
}
</style>
</head>
<body>
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</div>
<div class="swiper-slide">Slide 4</div>
<div class="swiper-slide">Slide 5</div>
<div class="swiper-slide">Slide 6</div>
<div class="swiper-slide">Slide 7</div>
<div class="swiper-slide">Slide 8</div>
<div class="swiper-slide">Slide 9</div>
<div class="swiper-slide">Slide 10</div>
</div>
<div class="swiper-pagination"></div>
</div>
<script src="../package/js/swiper.min.js"></script>
<script>
var swiper = new Swiper('.swiper-container', {
slidesPerView: 3,
slidesPerColumn: 2,
spaceBetween: 30,
pagination: {
el: '.swiper-pagination',
clickable: true,
},
});
</script>
</body>
</html>
我尝试更改每个视图的幻灯片和每列的幻灯片,但它们几乎相同。
更改容器高度
.swiper-container { height: 800px; }
我遇到了同样的问题。看来设置slidesPerColumn
不起作用。然后我用这些 Swiper Slide 设置检查哪个 CSS 来自 swiper css:
.swiper-container-multirow-column > .swiper-wrapper {
flex-direction: column;
}
事实证明,当我覆盖 CSS 时,它起作用了:
.swiper-container-multirow-column > .swiper-wrapper {
flex-direction: inherit;
}
或者,添加以下设置:
slidesPerColumnFill: 'row'
希望对您有所帮助(即使是关于此主题的较晚回答)。
如果您使用 CDN ->
<link rel="stylesheet"href="https://unpkg.com/swiper/swiper-bundle.min.css" >
<script src="https://unpkg.com/swiper/swiper-bundle.min.js"></script>
替换为 ->
<link rel="stylesheet"href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.4.6/css/swiper.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.4.6/js/swiper.min.js"></script>
这样就可以了。显然,许多选项不适用于以前的 CDN