gpt4 book ai didi

javascript - 一页上有两个滑动 slider

转载 作者:行者123 更新时间:2023-12-03 03:31:35 24 4
gpt4 key购买 nike

我正在使用swiper slider在我的页面上。

现在我想添加另一个。我被复制了第一个 slider 并更改了他的类,就像我对 js 初始化所做的一样。两个 slider 都工作,但在第二个 slider 中看起来配置参数被忽略...我有 1 个可见幻灯片而不是 10 个,spaceBetween 参数没有更改任何内容,我设置了 1 或 500 没有...

我做错了什么?

HTML:

<div class="swiper-container slider1">
<div class="swiper-wrapper">
<div class="swiper-slide">
slide 1
</div>
<div class="swiper-slide">
slide 2
</div>
...
</div>
<!-- Add Arrows -->
<div class="swiper-button-next"></div>
<div class="swiper-button-prev"></div>
</div>

<div class="swiper-container slider2">
<div class="swiper-wrapper">
<div class="swiper-slide">
slide 1
</div>
<div class="swiper-slide">
slide 2
</div>
...
</div>
<!-- Add Arrows -->
<div class="swiper-button-next"></div>
<div class="swiper-button-prev"></div>
</div>

JS:

var swiper = new Swiper('.slider1', {
slidesPerView: 3,
paginationClickable: true,
lazyLoading: true,
spaceBetween: 10,
nextButton: '.swiper-button-next',
prevButton: '.swiper-button-prev'
});

var newSwiper = new Swiper('.slider2', {
slidesPerView: 10,
paginationClickable: true,
lazyLoading: true,
spaceBetween: 10,
nextButton: '.swiper-button-next',
prevButton: '.swiper-button-prev'
});

编辑:

所以问题是 - html swiper slider 位于 bootstrap 类 tab-pane 中,它是 bootstrap tabs所以它是隐藏的,点击选项卡后它就会显示出来。

第二个 slider 的原始js:

var newSwiper = new Swiper('.slider2', {
slidesPerView: 10,
paginationClickable: true,
lazyLoading: true,
spaceBetween: 10,
nextButton: '.swiper-button-next',
prevButton: '.swiper-button-prev'
});

和解决方案:

$(".locality-tab").on("click",function(){ 
setTimeout(function () {
newSwiper.update();
}, 400);
});

.locality-tab 是切换内容的按钮类。

最佳答案

您的代码工作正常,检查下面的代码片段。

如果您有 1 张可见幻灯片,则 spaceBetween 配置没有任何意义。

如果您想使用 spaceBetween 配置,则需要将 slidesPerView 值设置为至少 2。

spaceBetween: Distance between slides in px.

slidesPerView: Number of slides per view (slides visible at the same time on slider's container).

var swiper = new Swiper('.slider1', {
slidesPerView: 2,
spaceBetween: 20, //it is only effective when slidesPerView >=2
paginationClickable: true,
lazyLoading: true,
nextButton: '.swiper-button-next',
prevButton: '.swiper-button-prev'
});

var newSwiper = new Swiper('.slider2', {
slidesPerView: 3,
paginationClickable: true,
spaceBetween: 5,
lazyLoading: true,
nextButton: '.swiper-button-next',
prevButton: '.swiper-button-prev'
});
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.4.2/css/swiper.min.css">
<style>
.swiper-slide{
color:white;
padding:10px;
}

</style>
</head>
<body>
<div class="swiper-container slider1 row">
<div class="swiper-wrapper col-md-12">
<div class="swiper-slide col-md-4 text-center" style="background-color:blue;">
slide 1
</div>
<div class="swiper-slide col-md-4 text-center"
style="background-color:black;">
slide 2
</div>
<div class="swiper-slide col-md-4 text-center"
style="background-color:yellow;">
slide 3
</div>
<div class="swiper-slide col-md-4 text-center"
style="background-color:red;">
slide 4
</div>
</div>
<!-- Add Arrows -->
<div class="swiper-button-next"></div>
<div class="swiper-button-prev"></div>
</div>
<br>
<br>
<div class="swiper-container slider2 row">
<div class="swiper-wrapper col-md-12">
<div class="swiper-slide col-md-4 text-center"
style="background-color:red;">
slide 1
</div>
<div class="swiper-slide col-md-4 text-center"
style="background-color:yellow;">
slide 2
</div>
<div class="swiper-slide col-md-4 text-center"
style="background-color:blue;">
slide 3
</div>
<div class="swiper-slide col-md-4 text-center"
style="background-color:green;">
slide 4
</div>
</div>
<!-- Add Arrows -->
<div class="swiper-button-next"></div>
<div class="swiper-button-prev"></div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.4.2/js/swiper.jquery.min.js"></script>
</body>
</html>

更多配置请访问Swiper Parameters

关于javascript - 一页上有两个滑动 slider ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46072100/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com