![](/img/trans.png)
[英]swiper.js | Stop swiper slide autoplay on mouse enter and start autoplay on mouse leave
[英]Stop swiper slide autoplay on mouse enter and start autoplay on mouse leave
如何在鼠标进入时停止滑动滑动自动播放并在鼠标离开时开始自动播放? 我试过.stopAutoplay()
和.startAutoplay()
function 但对我不起作用。
谢谢这里是代码。 我面临控制台错误
未捕获的类型错误:swiper.startAutoplay 不是 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();
});
您需要使用选项disableOnInteraction: true
而不是自己绑定事件,请参阅此处的文档。
您可以选择使用以下内容进行自动播放开始停止
swiper.autoplay.start();
swiper.autoplay.stop();
编辑
您的错误在于您如何获取 swiper 实例。 见下面的演示
$(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>
var swiper = new Swiper('.swiper-container', {
....
...
....
});
$(".swiper-container").hover(function() {
(this).swiper.autoplay.stop();
}, function() {
(this).swiper.autoplay.start();
});
同一页面上多个滑块的通用解决方案。
根据 swiper.js 版本 8 https://swiperjs.com/swiper-api#autoplay的文档
autoplay: {
disableOnInteraction: false,
pauseOnMouseEnter: true,
},
对于几种情况,唯一有效的代码如下:
$(".swiper-container").each(function(elem, target){ var swp = target.swiper; $(this).hover(function() { swp.autoplay.stop(); }, function() { swp.autoplay.start(); }); });
在 vuejs(使用vue-awesome-swiper
)中,我不得不用div
包裹swiper
组件,然后将事件侦听器添加到 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>
将事件侦听器直接放在组件上不起作用
对于带有来自swiper/vue
vue 组件的 Vue 3:
<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>
Swiper v6.6.0 (pauseOnMouseEnter) 中添加了新参数,因此从现在开始不需要解决方法: https ://swiperjs.com/swiper-api#autoplay
如果您对 swipper 使用 vuejs 指令: 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.