Было много споров по этому вопросу, первоначальный автор решил не реализовывать такую функцию, как эта, потому что горизонтальная бесконечная прокрутка не стоит дополнительной сложности.
Однако ...
После некоторой игры с приведенным выше кодом и работы над частичным решением, предоставленным koraysels , я придумал следующий (работающий!) Фрагмент:
const mainTicker = new Flickity('.js-main-slider', {
accessibility: true,
resize: true,
wrapAround: true,
prevNextButtons: false,
pageDots: false,
percentPosition: true,
setGallerySize: true,
});
// Set initial position to be 0
mainTicker.x = 0;
// Start the marquee animation
play();
// Main function that 'plays' the marquee.
function play() {
// Set the decrement of position x
mainTicker.x -= 1.5;
// Settle position into the slider
mainTicker.settle(mainTicker.x);
// Set the requestId to the local variable
requestId = window.requestAnimationFrame(play);
}
// Main function to cancel the animation.
function pause() {
if(requestId) {
// Cancel the animation
window.cancelAnimationFrame(requestId)
// Reset the requestId for the next animation.
requestId = undefined;
}
}
// Pause on hover/focus
$('.js-main-slider').on('mouseenter focusin', e => {
pause();
})
// Unpause on mouse out / defocus
$('.js-main-slider').on('mouseleave', e => {
play();
})
@import url('https://unpkg.com/flickity@2.1.2/dist/flickity.min.css');
.b-slider__slides {
width: 100%;
background: blue;
overflow: hidden;
}
.b-slider__slide {
margin: 0 2.5rem;
text-align: center;
background: red;
width: 30rem;
height: 20rem;
}
a {
display: flex;
flex-flow: column nowrap;
background: red;
color: #000;
text-decoration: underline;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://unpkg.com/flickity@2.1.2/dist/flickity.pkgd.min.js"></script>
<div class="b-slider__slides js-main-slider">
<div class="b-slider__slide">
<a href="#" class="b-slider__ref">
<picture class="b-slider__picture">
<img src="https://via.placeholder.com/350x150">
</picture>
<span class="b-slider__date">29/01/1993</span>
<h3 class="b-slider__subtitle">Lorem ipsum</h3>
</a>
</div>
<div class="b-slider__slide">
<a href="#" class="b-slider__ref">
<picture class="b-slider__picture">
<img src="https://via.placeholder.com/350x150">
</picture>
<span class="b-slider__date">29/01/1993</span>
<h3 class="b-slider__subtitle">Lorem ipsum</h3>
</a>
</div>
<div class="b-slider__slide">
<a href="#" class="b-slider__ref">
<picture class="b-slider__picture">
<img src="https://via.placeholder.com/350x150">
</picture>
<span class="b-slider__date">29/01/1993</span>
<h3 class="b-slider__subtitle">Lorem ipsum</h3>
</a>
</div>
<div class="b-slider__slide">
<a href="#" class="b-slider__ref">
<picture class="b-slider__picture">
<img src="https://via.placeholder.com/350x150">
</picture>
<span class="b-slider__date">29/01/1993</span>
<h3 class="b-slider__subtitle">Lorem ipsum</h3>
</a>
</div>
</div>
Возможно, вы захотите отключить перетаскивание, если хотите добиться эффекта чистого выделения, но он полностью подходит для моего варианта использования.