本文介绍了鼠标进入时停止滑动自动播放并在鼠标离开时开始自动播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在鼠标进入时停止滑动自动播放并在鼠标离开时开始自动播放?我尝试过 .stopAutoplay()
和 .startAutoplay()
函数,但对我不起作用.
谢谢这里是代码.我面临控制台错误
未捕获的类型错误:swiper .startAutoplay 不是函数
var swiper = new Swiper('.swiper-container', {分页:'.swiper-分页',分页可点击:真,nextButton: '.swiper-button-next',prevButton: '.swiper-button-prev',空间之间:0,循环:真实,效果:'幻灯片',longSwipes: 真,自动播放: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', {速度:400,空间之间:100,自动播放:真实,disableOnInteraction: 真,});var mySwiper = document.querySelector('.swiper-container').swiper$(".swiper-container").mouseenter(function() {mySwiper.autoplay.stop();console.log('滑块停止');});$(".swiper-container").mouseleave(function() {mySwiper.autoplay.start();console.log('滑块再次启动');});});
.swiper-slide {文本对齐:居中;}
<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"/><!-- 滑块主容器--><div class="swiper-container"><!-- 额外需要的包装器--><div class="swiper-wrapper"><!-- 幻灯片 --><div class="swiper-slide">幻灯片 1</div><div class="swiper-slide">幻灯片 2</div><div class="swiper-slide">幻灯片 3</div><div class="swiper-slide">幻灯片 4</div>
<!-- 如果我们需要分页--><div class="swiper-pagination"></div><!-- 如果我们需要导航按钮--><div class="swiper-button-prev"></div><div class="swiper-button-next"></div><!-- 如果我们需要滚动条--><div class="swiper-scrollbar"></div>
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
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();
});
解决方案
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>
这篇关于鼠标进入时停止滑动自动播放并在鼠标离开时开始自动播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!