[英]iDangerous Swiper, set slides to different timers
I cannot seem to get my swiper to have different times set to each individual slide. 我似乎无法让每张幻灯片的滑动时间都不同。 For example, if I had 5 slides, each slide would autoplay but based on different timers. 例如,如果我有5张幻灯片,则每张幻灯片会自动播放,但基于不同的计时器。 Slide 1 would be 5000ms, Slide 2 would be 10000ms, etc... 幻灯片1为5000毫秒,幻灯片2为10000毫秒,依此类推...
This is what I have so far but it doesn't seem to want to work. 到目前为止,这是我所拥有的,但似乎不想工作。
JS: - Method 1 JS: -方法1
var mySwiper = new Swiper('.swiper-container', {
nextButton: '.r_control',
prevButton: '.l_control',
slidesPerView: 1,
paginationClickable: true,
spaceBetween: 30,
autoplay: 5000,
autoplayDisableOnInteraction: false,
preloadImages: false, /* May not need */
lazyLoading: true, /* May not need */
loop: true,
onSlideChangeEnd: function ( swiper ) {
// Set individual timeout for autoplay
var currentSlideIndex = swiper.activeIndex,
timeout = $( swiper.slides[ currentSlideIndex ] ).data( "timeout" );
if ( timeout === undefined || timeout === '' || timeout === 0) {
timeout = 1000;
}
setTimeout( function () {
swiper.slideNext();
}, timeout );
}
});
HTML: - Used for both Methods HTML: -用于两种方法
<!-- Start of 'Slide #1' -->
<div class="swiper-slide" data-timeout="8000"> <!-- data-timeout specified here next to 'swiper-slide' class for each slide -->
<div class="swiper-slide_img">
<!-- Start of 'Link' -->
<a target="_blank" href="#">
<div class="image"></div>
</a>
<!-- End of 'Link' -->
</div>
</div>
<!-- End of 'Slide #1' -->
I've also tried this approach below but no luck. 我也在下面尝试了这种方法,但是没有运气。
JS: - Method 2 JS: -方法2
// Set individual slide timeout for dynamic autoplay
var setSwiperSlideTimeout = function ( swiper ) {
var timeout = $( swiper.slides[ swiper.activeIndex ] ).data( "timeout" );
if (timeout === undefined || timeout === "" || timeout === 0) {
timeout = 1000;
}
swiper.params.autoplay = timeout;
swiper.update();
swiper.startAutoplay();
};
var mySwiper = new Swiper('.swiper-container', {
nextButton: '.r_control',
prevButton: '.l_control',
slidesPerView: 1,
paginationClickable: true,
spaceBetween: 30,
autoplay: 5000,
autoplayDisableOnInteraction: false,
preloadImages: false, /* May not need */
lazyLoading: true, /* May not need */
loop: true,
onInit: function ( currentSwiper ) {
currentSwiper.stopAutoplay();
setSwiperSlideTimeout( currentSwiper );
},
onSlideChangeEnd: function ( currentSwiper ) {
currentSwiper.stopAutoplay();
setSwiperSlideTimeout( currentSwiper );
}
Why are these two methods not working? 为什么这两种方法不起作用? What am I doing wrong here? 我在这里做错了什么?
Figured it out. 弄清楚了。 Method 2 works BUT instead of hard-coding autoplay option to 5000, I had to set that to 0. Here is the full code for others having this same issue: 方法2可以使用BUT,而不是将autoplay硬编码选项设置为5000,我必须将其设置为0。这是具有相同问题的其他人的完整代码:
JS: JS:
// Set individual slide timeout for dynamic autoplay
var setSwiperSlideTimeout = function ( swiper ) {
var timeout = $( swiper.slides[ swiper.activeIndex ] ).data( "timeout" );
if (timeout === undefined || timeout === "" || timeout === 0) {
timeout = 1000;
}
swiper.params.autoplay = timeout;
swiper.update();
swiper.startAutoplay();
};
var mySwiper = new Swiper('.swiper-container', {
nextButton: '.r_control',
prevButton: '.l_control',
slidesPerView: 1,
paginationClickable: true,
spaceBetween: 30,
autoplay: 0, // CHANGED THIS FROM 5000 to 0
autoplayDisableOnInteraction: false,
preloadImages: false, /* May not need */
lazyLoading: true, /* May not need */
loop: true,
onInit: function ( currentSwiper ) {
currentSwiper.stopAutoplay();
setSwiperSlideTimeout( currentSwiper );
},
onSlideChangeEnd: function ( currentSwiper ) {
currentSwiper.stopAutoplay();
setSwiperSlideTimeout( currentSwiper );
}
HTML: HTML:
<!-- Start of 'Slide #1' -->
<div class="swiper-slide" data-timeout="8000"> <!-- data-timeout specified here next to 'swiper-slide' class for each slide -->
<div class="swiper-slide_img">
<!-- Start of 'Link' -->
<a target="_blank" href="#">
<div class="image"></div>
</a>
<!-- End of 'Link' -->
</div>
</div>
<!-- End of 'Slide #1' -->
iDangerous recently made available a native parameter to easily allow individual delay configurations. iDangerous最近提供了一个本机参数,可以轻松地进行单独的延迟配置。 You may set a generic autoplay delay in the swiper code: 您可以在滑动代码中设置通用的自动播放延迟:
var mySwiper = new Swiper ('.swiper-container', {
speed: 300,
autoplay: {
delay: 5000,
},
});
And set specific ones in the targeted slides as well, like the following: 并在目标幻灯片中设置特定的幻灯片,如下所示:
<div class="swiper-slide" data-swiper-autoplay="2000">Content</div>
The individual statements will only work if autoplay is set in the swiper code. 仅当在滑动代码中设置了自动播放时,单个语句才起作用。
I am not sure which release received this new parameter, thus you may want to download the last one available. 我不确定哪个版本收到了这个新参数,因此您可能要下载最后一个可用的参数。 (Working on v.4.5.0) (正在处理v.4.5.0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.