最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Swiper slider error startAutoplay is not a function - Stack Overflow

programmeradmin2浏览0评论

I got a problem with swiper slider. I would like my swiper stop on mouse enter and continue on mouseleave. But my console shows me an error --> swiper.stopAutoplay is not a function, but the console log is displayed. And the same with startAutoplay. Does anybody know what am I doing wrong?

<script>
    var swiper = new Swiper('.swiper-container', {
      loop: true,
      speed:2000,
      autoplay: {
        delay: 3500,
      },
      pagination: {
        el: '.swiper-pagination',
      },
    });

(function($) {
$('.swiper-container').on('mouseenter', function(e){
    console.log('stop autoplay');
    swiper.stopAutoplay();
  })

  $('.swiper-container').on('mouseleave', function(e){
    console.log('start autoplay');
    swiper.startAutoplay();
  })
})(jQuery);


  </script>

I got a problem with swiper slider. I would like my swiper stop on mouse enter and continue on mouseleave. But my console shows me an error --> swiper.stopAutoplay is not a function, but the console log is displayed. And the same with startAutoplay. Does anybody know what am I doing wrong?

<script>
    var swiper = new Swiper('.swiper-container', {
      loop: true,
      speed:2000,
      autoplay: {
        delay: 3500,
      },
      pagination: {
        el: '.swiper-pagination',
      },
    });

(function($) {
$('.swiper-container').on('mouseenter', function(e){
    console.log('stop autoplay');
    swiper.stopAutoplay();
  })

  $('.swiper-container').on('mouseleave', function(e){
    console.log('start autoplay');
    swiper.startAutoplay();
  })
})(jQuery);


  </script>
Share Improve this question asked Dec 21, 2017 at 8:30 Marcin MroczkoMarcin Mroczko 1431 silver badge16 bronze badges 2
  • Ok. I figured it out :) It should be swiper.stop.autoplay & swiper.start.autoplay – Marcin Mroczko Commented Dec 21, 2017 at 9:37
  • It's actually: swiper.autoplay.stop() and swiper.autoplay.start(). – nick Commented Sep 3, 2018 at 13:24
Add a ment  | 

2 Answers 2

Reset to default 4

In version 4.3.5 you must use autoplay.stop and autoplay.start. For example:

var mySwiper = new Swiper('.my-swiper');
$('.my-swiper').hover(function() {
    mySwiper.autoplay.stop();
}, function() {
    mySwiper.autoplay.start();
});

I'm running 5.1.0. After console.logging swiper reference i saw that for each tab was specific one created. So in the loop when i was hiding the tabs just needed to:

swiper[i].autoplay.stop();

And on tab click showing the tab:

swiper[i].autoplay.start();

So thanks to answer above by @nick i was able to get this running.

发布评论

评论列表(0)

  1. 暂无评论