在鼠标进入时停止滑动滑动自动播放并在鼠标离开时开始自动播放
Stop swiper slide autoplay on mouse enter and start autoplay on mouse leave
如何在鼠标进入时停止滑动滑动自动播放并在鼠标离开时开始自动播放?我尝试了 .stopAutoplay()
和 .startAutoplay()
功能,但对我不起作用。
谢谢,这是代码。我遇到控制台错误
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();
});
您需要使用选项 disableOnInteraction: true
而不是自己绑定事件 documentation。
您可以选择使用以下内容进行自动播放开始停止
swiper.autoplay.start();
swiper.autoplay.stop();
编辑
你的错误是你如何获得刷卡器的实例。请参阅下面的演示
$(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();
});
同一页面上多个滑块的通用解决方案。
在 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-container").each(function(elem, target){
var swp = target.swiper;
$(this).hover(function() {
swp.autoplay.stop();
}, function() {
swp.autoplay.start();
});
});
如果你对 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>
对于具有来自 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
根据 swiper.js 版本 8 的文档
https://swiperjs.com/swiper-api#autoplay
autoplay: {
disableOnInteraction: false,
pauseOnMouseEnter: true,
},
如何在鼠标进入时停止滑动滑动自动播放并在鼠标离开时开始自动播放?我尝试了 .stopAutoplay()
和 .startAutoplay()
功能,但对我不起作用。
谢谢,这是代码。我遇到控制台错误
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();
});
您需要使用选项 disableOnInteraction: true
而不是自己绑定事件 documentation。
您可以选择使用以下内容进行自动播放开始停止
swiper.autoplay.start();
swiper.autoplay.stop();
编辑
你的错误是你如何获得刷卡器的实例。请参阅下面的演示
$(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();
});
同一页面上多个滑块的通用解决方案。
在 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-container").each(function(elem, target){
var swp = target.swiper;
$(this).hover(function() {
swp.autoplay.stop();
}, function() {
swp.autoplay.start();
});
});
如果你对 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>
对于具有来自 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
根据 swiper.js 版本 8 的文档 https://swiperjs.com/swiper-api#autoplay
autoplay: {
disableOnInteraction: false,
pauseOnMouseEnter: true,
},