在鼠标离开时停止swiper slide autoplay,并在鼠标离开时启动自动播放

时间:2017-11-11 13:11:02

标签: javascript jquery swiper

如何在鼠标离开时停止swiper slide autoplay并在鼠标离开时启动自动播放?我尝试了.stopAutoplay().startAutoplay()功能,但没有为我工作。

谢谢你这里是代码。我面临控制台错误

  

未捕获的TypeError:swiper .startAutoplay不是函数

 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();
});

7 个答案:

答案 0 :(得分:4)

您需要使用disableOnInteraction: true选项,而不是自己在documentation看到的事件绑定。

您可以选择使用以下内容进行自动播放开始停止

  • 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>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

dicto = {12:{34:45,56:78},45:{67:23,90:15}}
for k,v in dicto.items():
    for i in v.keys():
        if i == 67:
            del v[i]

print(dicto)

同一页面上多个滑块的通用解决方案。

答案 2 :(得分:1)

在vuejs(带有vue-awesome-swiper)中,我不得不用swiper包装div组件,然后将事件侦听器添加到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>

直接将事件侦听器放置在组件上不起作用

答案 3 :(得分:1)

对于带有来自 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>

答案 4 :(得分:0)

在某些情况下,唯一的代码如下:

$(".swiper-container").each(function(elem, target){
    var swp = target.swiper;
    $(this).hover(function() {
        swp.autoplay.stop();
    }, function() {
        swp.autoplay.start();
    });
});

答案 5 :(得分:0)

如果您对vipper使用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>

答案 6 :(得分:0)

Swiper v6.6.0 新增参数(pauseOnMouseEnter),因此从现在开始不需要解决方法: https://swiperjs.com/swiper-api#autoplay