我正在使用Swiper Slider。在单击“下一个/上一个”按钮后,是否可以在幻灯片更改之前添加延迟?例如:我单击下一步按钮,等待1秒钟,然后切换到下一张幻灯片。我需要这样做,因为我已经使用CSS制作了自己的动画,并且需要使用aprox。 1秒才能完全完成。
希望你们能帮我这个忙。
代码如下:
var swiper = new Swiper('.swiper-container', {
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
});
.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;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<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>
<!-- Add Arrows -->
<div class="swiper-button-next"></div>
<div class="swiper-button-prev"></div>
</div>
答案 0 :(得分:0)
这可以按照您的要求进行。
通过删除导航定义,我们可以为下一个和上一个按钮编写自己的操作。
我用setTimeout
等待指定的时间,然后从您的库中启动了内置的next / previous函数。
我还添加了一些CSS,以便在此期间按钮显示为禁用状态,以便用户理解应该再次单击。
// Initialise swiper
var swiper = new Swiper('.swiper-container');
// Define delay (in milliseconds)
swiperDelay = 2000;
// Add event to previous button
$('.swiper-button-prev.enabled').click(function(){
// Disable swiper buttons so user doesnt click again
$(".swiper-button").removeClass("enabled");
// Set timeout for previous slide move
setTimeout(function(){
// Move to previous slide
swiper.slidePrev();
// Re-enable swiper buttons
$(".swiper-button").addClass("enabled");
}, swiperDelay);
});
// Add event to next button
$('.swiper-button-next.enabled').click(function(){
// Disable swiper buttons so user doesnt click again
$(".swiper-button").removeClass("enabled");
// Set timeout for next slide move
setTimeout(function(){
// Move to next slide
swiper.slideNext();
// Re-enable swiper buttons
$(".swiper-button").addClass("enabled");
}, swiperDelay);
});
.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;
}
.swiper-button {
opacity: 0.4;
transition: 0.3s all;
}
.swiper-button.enabled {
opacity: 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.4.6/js/swiper.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.4.6/css/swiper.min.css" />
<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>
<!-- Add Arrows -->
<div class="swiper-button-next enabled swiper-button"></div>
<div class="swiper-button-prev enabled swiper-button"></div>
</div>