Swiper Autoplay Pause on Hover
January 22, 2025 · AI Conversations
slidesPerView: 3,
spaceBetween: 30,
loop: true,
speed: 8000,
autoplay: {
delay:0,// Set a short delay for better autoplay handling
disableOnInteraction: true,
},
on: {
beforeInit: function (swiper) {
swiper.wrapperEl.style.transitionTimingFunction = "linear";
},
slideChangeTransitionEnd: function () {
swiper.autoplay.start(); // Ensure autoplay continues
},
},
breakpoints: {
0: {
slidesPerView: 1.2,
spaceBetween: 10,
},
1024: {
slidesPerView: 3,
spaceBetween: 30,
},
},
});
Now its working continous scrolling smoothess
now I wan to pause when use mouse over on it
var swiper = new Swiper(".swiperslides", {
slidesPerView: 3,
spaceBetween: 30,
loop: true,
speed: 8000,
autoplay: {
delay: 0, // Set a short delay for better autoplay handling
disableOnInteraction: true,
},
on: {
beforeInit: function (swiper) {
swiper.wrapperEl.style.transitionTimingFunction = "linear";
},
slideChangeTransitionEnd: function () {
swiper.autoplay.start(); // Ensure autoplay continues
},
},
breakpoints: {
0: {
slidesPerView: 1.2,
spaceBetween: 10,
},
1024: {
slidesPerView: 3,
spaceBetween: 30,
},
},
});
// Pause autoplay on mouse enter
swiper.el.addEventListener('mouseenter', function () {
swiper.autoplay.stop();
});
// Resume autoplay on mouse leave
swiper.el.addEventListener('mouseleave', function () {
swiper.autoplay.start();
});
### Explanation:
– **`swiper.el.addEventListener('mouseenter', …)`**: This adds an event listener that stops the autoplay when the mouse enters the Swiper's container.
– **`swiper.el.addEventListener('mouseleave', …)`**: This adds an event listener that resumes the autoplay when the mouse leaves the Swiper's container.
With this setup, the autoplay will pause when a user hovers over the Swiper and will resume when the mouse leaves.
var swiper = new Swiper(".swiperslides", {
slidesPerView: 3,
spaceBetween: 30,
loop: true,
speed: 8000,
autoplay: {
delay: 0, // Set a short delay for better autoplay handling
disableOnInteraction: false, // Allow interaction to pause the autoplay
},
on: {
beforeInit: function (swiper) {
swiper.wrapperEl.style.transitionTimingFunction = "linear";
},
slideChangeTransitionEnd: function () {
swiper.autoplay.start(); // Ensure autoplay continues
},
},
breakpoints: {
0: {
slidesPerView: 1.2,
spaceBetween: 10,
},
1024: {
slidesPerView: 3,
spaceBetween: 30,
},
},
});
// Pause autoplay on mouse enter
swiper.el.addEventListener('mouseenter', function () {
swiper.autoplay.stop();
});
// Resume autoplay on mouse leave
swiper.el.addEventListener('mouseleave', function () {
swiper.autoplay.start();
});
### Key Changes:
1. **`disableOnInteraction: false`**: This allows autoplay to continue even after user interactions, ensuring that your manual pause and resume logic works.
2. **Event Listeners**: The event listeners for `mouseenter` and `mouseleave` are added to `swiper.el`, which is the main Swiper container.
### Additional Debugging Steps:
– Make sure you include the Swiper CSS and JS files correctly in your HTML.
– Check the console for any JavaScript errors that might indicate issues with Swiper initialization.
– Verify that the class `.swiperslides` is applied to the correct Swiper container.
If the autoplay still doesn’t pause on hover, you can also try using Swiper's built-in event handling for better compatibility:
var swiper = new Swiper(".swiperslides", {
// Your existing configuration
});
// Using Swiper's built-in events
swiper.on('mouseenter', function () {
swiper.autoplay.stop();
});
swiper.on('mouseleave', function () {
swiper.autoplay.start();
});
This should ensure that the autoplay pauses when the mouse hovers over the Swiper and resumes when it leaves. If you continue to experience issues, please provide any error messages or additional context for further troubleshooting.