Skip to content

Instantly share code, notes, and snippets.

@zoxon
Last active September 18, 2018 10:02
Show Gist options
  • Save zoxon/a8577914fdedfec36773e020e48a057f to your computer and use it in GitHub Desktop.
Save zoxon/a8577914fdedfec36773e020e48a057f to your computer and use it in GitHub Desktop.
Swiper.js - stop autoplay on hover and start on blur
const sliderElement = document.querySelector(".main-slider");
if (sliderElement) {
const slider = new Swiper(".swiper-container", {
slidesPerView: 1,
spaceBetween: 0,
watchOverflow: true,
loop: true,
autoplay: {
delay: 4000,
disableOnInteraction: false
},
pagination: {
el: ".swiper-pagination"
}
});
["mouseenter", "mouseleave"].forEach(eventName =>
sliderElement.addEventListener(
eventName,
() => {
slider.autoplay.running
? slider.autoplay.stop()
: slider.autoplay.start();
},
false
)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment