'Stop swiper slide autoplay on mouse enter and start autoplay on mouse leave
How to stop swiper slide autoplay on mouse enter and start autoplay on mouse leave? I have tried .stopAutoplay()
and .startAutoplay()
function but not worked for me.
thank you here is code. and i face console error
Uncaught TypeError: swiper .startAutoplay is not a function
var swiper = new Swiper('.swiper-container', {
pagination: '.swiper-pagination',
paginationClickable: true,
nextButton: '.swiper-button-next',
prevButton: '.swiper-button-prev',
spaceBetween: 0,
loop: true,
effect: 'slide',
longSwipes: true,
autoplay:2000,
autoplayDisableOnInteraction:true,
});
$(".swiper-container").mouseenter(function(){
swiper.stopAutoplay();
});
$(".swiper-container").mouseleave(function(){
swiper.startAutoplay();
});
Solution 1:[1]
You need to use the option disableOnInteraction: true
rather than binding the events yourself see here for documentation.
Optionally you can use the following for autoplay start stop
swiper.autoplay.start();
swiper.autoplay.stop();
Edit
Your mistake is how you are getting the instance for swiper. see below for demo
$(document).ready(function() {
new Swiper('.swiper-container', {
speed: 400,
spaceBetween: 100,
autoplay: true,
disableOnInteraction: true,
});
var mySwiper = document.querySelector('.swiper-container').swiper
$(".swiper-container").mouseenter(function() {
mySwiper.autoplay.stop();
console.log('slider stopped');
});
$(".swiper-container").mouseleave(function() {
mySwiper.autoplay.start();
console.log('slider started again');
});
});
.swiper-slide {
text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.0.5/js/swiper.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.0.5/css/swiper.css" rel="stylesheet" />
<!-- Slider main container -->
<div class="swiper-container">
<!-- Additional required wrapper -->
<div class="swiper-wrapper">
<!-- Slides -->
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</div>
<div class="swiper-slide">Slide 4</div>
</div>
<!-- If we need pagination -->
<div class="swiper-pagination"></div>
<!-- If we need navigation buttons -->
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
<!-- If we need scrollbar -->
<div class="swiper-scrollbar"></div>
</div>
Solution 2:[2]
var swiper = new Swiper('.swiper-container', {
....
...
....
});
$(".swiper-container").hover(function() {
(this).swiper.autoplay.stop();
}, function() {
(this).swiper.autoplay.start();
});
generic solution for multiple sliders on same page.
Solution 3:[3]
For several instances the only code works is the following:
$(".swiper-container").each(function(elem, target){
var swp = target.swiper;
$(this).hover(function() {
swp.autoplay.stop();
}, function() {
swp.autoplay.start();
});
});
Solution 4:[4]
New parameter has been added to Swiper v6.6.0 (pauseOnMouseEnter), so there is no need for a workaround from now: https://swiperjs.com/swiper-api#autoplay
Solution 5:[5]
In vuejs (with vue-awesome-swiper
), I had to wrap the swiper
component with a div
and then added the event listeners to the div.
<v-flex @mouseenter="$refs.swiperRef.swiper.autoplay.stop()"
@mouseleave="$refs.swiperRef.swiper.autoplay.start()">
<swiper :options="swiperOptions"
ref="swiperRef">
<swiper-slide v-for="(product, index) in products"
:key="index">
<!-- -->
</swiper-slide>
</swiper>
</v-flex>
Putting the event listeners on the component directly did not work
Solution 6:[6]
According to the documentation of swiper.js version 8 https://swiperjs.com/swiper-api#autoplay
autoplay: {
disableOnInteraction: false,
pauseOnMouseEnter: true,
},
Solution 7:[7]
For Vue 3 with vue components from swiper/vue
:
<template>
<swiper
:slides-per-view="1"
@swiper="onSwiper"
@mouseenter="() => this.swiper.autoplay.stop()"
@mouseleave="() => this.swiper.autoplay.start()"
>
<template v-for="pic in pictures">
<swiper-slide>
<figure>
<div v-html="pic.html"></div>
<figcaption>{{ pic.title }}</figcaption>
</figure>
</swiper-slide>
</template>
</swiper>
</template>
<script>
// Import Swiper Vue.js components
import { Swiper, SwiperSlide } from 'swiper/vue';
// Import Swiper styles
import 'swiper/swiper.scss';
export default {
components: {
Swiper,
SwiperSlide,
},
data() {
return {
swiper: null,
}
},
methods: {
onSwiper(swiper) {
this.swiper = swiper;
},
},
}
</script>
Solution 8:[8]
In case you are using vuejs directive for swipper : vue-awesome-swiper
<div class="swiper-container" v-swiper:mySwiper="swiperOptions" @mouseenter="stopSwip($event)" @mouseleave="startSwip($event)">
....
</div>
<script>
export default {
methods: {
stopSwip(event) {
event.target.swiper.autoplay.stop();
},
startSwip(event) {
event.target.swiper.autoplay.start();
},
},
}
</script>
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 | |
Solution 2 | Mitul Modi |
Solution 3 | markcanals |
Solution 4 | Piotr Koz?owski |
Solution 5 | |
Solution 6 | Umair Qazi |
Solution 7 | Michael Bolli |
Solution 8 | Ahmad Mobaraki |