初始化后如何禁用 Flickity 滑块的自动播放?
How to disable autoplay for Flickity slider after initialization?
我想从使用 Flickity 将自动播放选项设置为 true 制作的图像滑块开始。一段时间后,我想禁用该设置。这样做的原因是因为我对滑块的配置非常复杂,我只想确保用户知道它们是如何工作的,然后允许他们随心所欲地使用它们。我知道滑块在悬停时会停止自动播放,但这并不是我想要的。我尝试使用 setTimeout() 函数来处理此问题,但没有成功。
LINK TO SIMILAR ISSUE ON FLICKITY GIT REPOSITORY
HTML:
<div class="top-carousel top-carousel-food">
<div class="carousel-cell">
{{ 'food--1.jpg' | asset_url | img_tag }}
</div>
<div class="carousel-cell">
{{ 'food--2.jpg' | asset_url | img_tag }}
</div>
<div class="carousel-cell">
{{ 'food--3.jpg' | asset_url | img_tag }}
</div>
<div class="carousel-cell">
{{ 'food--4.jpg' | asset_url | img_tag }}
</div>
<div class="carousel-cell">
{{ 'food--5.jpg' | asset_url | img_tag }}
</div>
<div class="carousel-cell">
{{ 'food--6.jpg' | asset_url | img_tag }}
</div>
</div>
<div class="bottom-carousel bottom-carousel-food">
<div class="carousel-cell">
{{ 'food--1.jpg' | asset_url | img_tag }}
</div>
<div class="carousel-cell">
{{ 'food--2.jpg' | asset_url | img_tag }}
</div>
<div class="carousel-cell">
{{ 'food--3.jpg' | asset_url | img_tag }}
</div>
<div class="carousel-cell">
{{ 'food--4.jpg' | asset_url | img_tag }}
</div>
<div class="carousel-cell">
{{ 'food--5.jpg' | asset_url | img_tag }}
</div>
<div class="carousel-cell">
{{ 'food--6.jpg' | asset_url | img_tag }}
</div>
</div>
jQuery:
$( document ).ready(function() {
$('.top-carousel-food').flickity({
// options
cellAlign: 'left',
contain: true,
lazyLoad: true,
sync: '.bottom-carousel-food'
});
$('.bottom-carousel').flickity({
// options
cellAlign: 'left',
contain: true,
lazyLoad: true,
pageDots: false,
wrapAround: true,
autoPlay: true
});
if ($(window).width() > 767) {
$('.top-carousel-food').flickity({
draggable: false,
pageDots: false,
prevNextButtons: false
});
}
var $gallery = $('.bottom-carousel-food').flickity();
$gallery.on( 'staticClick.flickity', function( event, pointer, cellElem, cellIndex ) {
if ( cellIndex !== undefined ) {
$gallery.flickity( 'select', cellIndex );
}
});
$('.bottom-carousel').flickity( options );
setTimeout(function() {
options.autoPlay = false;
}, 5000);
});
// external js: flickity.pkgd.js
var $carousel = $('.carousel').flickity({
autoPlay: true,
});
var flkty = $carousel.data('flickity');
var autoplayCount = 0;
$carousel.on( 'select.flickity', onSelect );
function onSelect() {
if ( flkty.player.state != 'playing' ) {
disableAutoplay()
return;
}
// stop after 2 advances
autoplayCount++;
if ( autoplayCount >= 2 ) {
disableAutoplay();
}
}
function disableAutoplay() {
$carouse.flickity('stopPlayer');
$carousel.off( 'select.flickity', onSelect );
}
我想从使用 Flickity 将自动播放选项设置为 true 制作的图像滑块开始。一段时间后,我想禁用该设置。这样做的原因是因为我对滑块的配置非常复杂,我只想确保用户知道它们是如何工作的,然后允许他们随心所欲地使用它们。我知道滑块在悬停时会停止自动播放,但这并不是我想要的。我尝试使用 setTimeout() 函数来处理此问题,但没有成功。
LINK TO SIMILAR ISSUE ON FLICKITY GIT REPOSITORY
HTML:
<div class="top-carousel top-carousel-food">
<div class="carousel-cell">
{{ 'food--1.jpg' | asset_url | img_tag }}
</div>
<div class="carousel-cell">
{{ 'food--2.jpg' | asset_url | img_tag }}
</div>
<div class="carousel-cell">
{{ 'food--3.jpg' | asset_url | img_tag }}
</div>
<div class="carousel-cell">
{{ 'food--4.jpg' | asset_url | img_tag }}
</div>
<div class="carousel-cell">
{{ 'food--5.jpg' | asset_url | img_tag }}
</div>
<div class="carousel-cell">
{{ 'food--6.jpg' | asset_url | img_tag }}
</div>
</div>
<div class="bottom-carousel bottom-carousel-food">
<div class="carousel-cell">
{{ 'food--1.jpg' | asset_url | img_tag }}
</div>
<div class="carousel-cell">
{{ 'food--2.jpg' | asset_url | img_tag }}
</div>
<div class="carousel-cell">
{{ 'food--3.jpg' | asset_url | img_tag }}
</div>
<div class="carousel-cell">
{{ 'food--4.jpg' | asset_url | img_tag }}
</div>
<div class="carousel-cell">
{{ 'food--5.jpg' | asset_url | img_tag }}
</div>
<div class="carousel-cell">
{{ 'food--6.jpg' | asset_url | img_tag }}
</div>
</div>
jQuery:
$( document ).ready(function() {
$('.top-carousel-food').flickity({
// options
cellAlign: 'left',
contain: true,
lazyLoad: true,
sync: '.bottom-carousel-food'
});
$('.bottom-carousel').flickity({
// options
cellAlign: 'left',
contain: true,
lazyLoad: true,
pageDots: false,
wrapAround: true,
autoPlay: true
});
if ($(window).width() > 767) {
$('.top-carousel-food').flickity({
draggable: false,
pageDots: false,
prevNextButtons: false
});
}
var $gallery = $('.bottom-carousel-food').flickity();
$gallery.on( 'staticClick.flickity', function( event, pointer, cellElem, cellIndex ) {
if ( cellIndex !== undefined ) {
$gallery.flickity( 'select', cellIndex );
}
});
$('.bottom-carousel').flickity( options );
setTimeout(function() {
options.autoPlay = false;
}, 5000);
});
// external js: flickity.pkgd.js
var $carousel = $('.carousel').flickity({
autoPlay: true,
});
var flkty = $carousel.data('flickity');
var autoplayCount = 0;
$carousel.on( 'select.flickity', onSelect );
function onSelect() {
if ( flkty.player.state != 'playing' ) {
disableAutoplay()
return;
}
// stop after 2 advances
autoplayCount++;
if ( autoplayCount >= 2 ) {
disableAutoplay();
}
}
function disableAutoplay() {
$carouse.flickity('stopPlayer');
$carousel.off( 'select.flickity', onSelect );
}