'How to make autoplay of the Swiper slider start only after the slider enters viewport?
I'm using this code to initialize swiper slider.
var mySwiper = new Swiper ('.swiper-container', {
// Optional parameters
pagination: '.swiper-pagination',
paginationClickable: true,
nextButton: '.swiper-button-next',
prevButton: '.swiper-button-prev',
spaceBetween: 0,
parallax: true,
autoplay: 5000,
speed: 800,
autoplayDisableOnInteraction: false
})
Since the slider is positioned inside the fourth section of the page and is visible only after the page is scrolled down, I would like to make the autoplay start only after the slider enters the viewport. Is there a way to do this?
Solution 1:[1]
var mySwiper = new Swiper('.swiper-container', {
autoplay: {
delay: 5000,
},
});
Solution 2:[2]
Assuming you're trying play on 4th slides:
var mySwiper = new Swiper ('.swiper-container', { // Optional parameters pagination: '.swiper-pagination', paginationClickable: true, nextButton: '.swiper-button-next', prevButton: '.swiper-button-prev', spaceBetween: 0, parallax: true, autoplay: 5000, speed: 800, autoplayDisableOnInteraction: false, onSlideChangeStart: function(s){ if (s.activeIndex === 3) { // do something here, 4th slide is active now and so on console.log('hi! Try to reach 4th slides'); s.startAutoplay(); // calling autoplay on 4th slides. } } })
Solution 3:[3]
You could potentially use something like jquery appear - https://github.com/morr/jquery.appear
$('mySwiperContainer').on('appear', function(event, $all_appeared_elements) {
// this element is now inside browser viewport
var mySwiper = new Swiper ('.swiper-container', {
// Optional parameters
pagination: '.swiper-pagination',
paginationClickable: true,
nextButton: '.swiper-button-next',
prevButton: '.swiper-button-prev',
spaceBetween: 0,
parallax: true,
autoplay: 5000,
speed: 800,
autoplayDisableOnInteraction: false
})
});
Solution 4:[4]
Object with autoplay
parameters needs to be used, or just boolean true
to enable with default settings to enable autoplay. Here is an example with delay
(between transitions in ms) parameter:
const swiper = new Swiper('.swiper', {
autoplay: {
delay: 5000,
},
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | NoAngel |
Solution 2 | Adam |
Solution 3 | dmoo |
Solution 4 | viking |