vue 中 鼠标移入停止轮播 移出开始轮播 (swiper5)

该博客介绍了如何使用 Vue 和 Swiper 组件创建一个带有鼠标enter/mouseleave事件的轮播图,实现用户鼠标悬停暂停和继续自动播放功能。代码展示了如何配置 Swiper 的选项,包括自动播放设置和导航按钮。
<template>
  <div class="swiper">
    <el-row :gutter="10" class="row-col">
      <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="col-info">
        /* 添加 移入移出的事件 */
        <swiper
          :options="swiperOptionInfo"
          ref="mySwipers"
          class="info-swiper"
          @mouseenter.native="mouseEnter"
          @mouseleave.native="mouseLeave"
        >
          <swiper-slide
            v-for="item in datas"
            :key="item.id"
            class="swiper-item"
          >
            <img :src="item.img" alt="" class="img" />
          </swiper-slide>
          <!-- <div slot="pagination" class="swiper-pagination"></div> -->
          <div
            slot="button-prev"
            class="swiper-button-prev hidden-sm-and-down"
          ></div>
          <div
            slot="button-next"
            class="swiper-button-next hidden-sm-and-down"
          ></div>
        </swiper>
      </el-col>
    </el-row>
  </div>
</template>
export default {
  name: "Swipers",
  components: {},
  props: ["datas"],
  data() {
    return {
      swiperOptionInfo: {
        loop: true,
        autoplay: {
          delay: 6000,
          stopOnLastSlide: false,
          disableOnInteraction: true,
        },
        // pagination: {
        //     el: '.swiper-pagination',
        //     clickable: true,
        // },
        navigation: {
          nextEl: ".swiper-button-next",
          prevEl: ".swiper-button-prev",
        },
      },
    };
  },
  methods: {
    mouseEnter() {
      this.$refs.mySwipers.$swiper.autoplay.stop();
    },
    mouseLeave() {
      this.$refs.mySwipers.$swiper.autoplay.start();
    },
  },
};
.swiper {
  margin-top: 100px;
  .row-col {
    max-height: 650px;
    .col-info {
      height: 100%;
      .info-swiper {
        height: 100%;
        .swiper-item {
          width: 100%;
          max-height: 550px;
        //   cursor: pointer;
          .img {
            width: 100%;
            height: 100%;
          }
        }
      }
    }
  }
}
<think>我们正在使用Vue3Swiper库来实现一个轮播图,并希望实现鼠标移入暂停轮播移出继续轮播的功能。 根据Swiper的官方文档,我们可以使用autoplay模块来实现自动轮播,并且该模块提供了autoplay.start()和autoplay.stop()方法来控制自动轮播的启动和停止。 在Vue3中,我们可以通过ref来获取Swiper实例,然后通过事件监听来实现暂停和继续。 步骤: 1. 安装Swiper(如果尚未安装): ```bash npm install swiper ``` 2.Vue组件中引入Swiper及其模块(如Autoplay)和样式。 3. 在模板中创建Swiper结构。 4. 在setup中使用ref创建swiper实例的引用。 5. 使用Swiper的on方法监听事件(或者直接在模板上绑定鼠标事件),这里我们可以在模板上绑定鼠标移入移出事件。 6.事件处理函数中,通过swiper实例调用autoplay的start和stop方法。 注意:Swiper 7+版本中,我们使用模块化的方式,需要引入Autoplay模块,并在Swiper初始化时传入该模块。 示例代码: 首先,确保安装了最新版本的Swiper(本文以Swiper 8为例,但原理适用于7+)。 组件代码示例:</think>在 Vue3 中实现 Swiper 轮播图的鼠标移入暂停功能,可以通过以下步骤实现: ### 解决方案步骤 1. **安装依赖** ```bash npm install swiper@8.0.0 vue-awesome-swiper@4.1.1 ``` 2. **组件实现** ```vue <template> <!-- 绑定鼠标事件 --> <div @mouseenter="pauseAutoplay" @mouseleave="resumeAutoplay"> <swiper :modules="modules" :autoplay="autoplayConfig" :loop="true" ref="swiperRef" > <swiper-slide v-for="(slide, index) in slides" :key="index"> <img :src="slide.image" alt="轮播图"> </swiper-slide> </swiper> </div> </template> <script setup> import { ref } from &#39;vue&#39;; import { Swiper, SwiperSlide } from &#39;swiper/vue&#39;; import { Autoplay } from &#39;swiper&#39;; // 引入自动播放模块 import &#39;swiper/css&#39;; // 核心样式 const swiperRef = ref(null); const modules = [Autoplay]; // 自动播放配置(初始启用) const autoplayConfig = ref({ delay: 3000, disableOnInteraction: false, // 用户操作后不停止 pauseOnMouseEnter: true // 鼠标移入暂停 }); const slides = [ { image: &#39;image1.jpg&#39; }, { image: &#39;image2.jpg&#39; }, { image: &#39;image3.jpg&#39; } ]; // 手动控制方法 const pauseAutoplay = () => { if (swiperRef.value?.$swiper?.autoplay) { swiperRef.value.$swiper.autoplay.pause(); } }; const resumeAutoplay = () => { if (swiperRef.value?.$swiper?.autoplay) { swiperRef.value.$swiper.autoplay.resume(); } }; </script> ``` ### 关键实现说明 1. **事件绑定** - 在包裹容器上绑定 `@mouseenter` 和 `@mouseleave` 事件 - 使用 `pauseOnMouseEnter: true` 启用 Swiper 内置暂停功能[^1] 2. **手动控制** - 通过 `swiperRef.value.$swiper.autoplay.pause()` 暂停 - 通过 `swiperRef.value.$swiper.autoplay.resume()` 恢复 - 使用 `disableOnInteraction: false` 确保用户操作后不会停止自动播放[^2] 3. **模块引入** - 必须引入 `Autoplay` 模块才能使用自动播放功能 - 配置项中的 `delay` 控制自动轮播间隔时间 ### 注意事项 1. Swiper 版本需使用 8.x+,旧版 API 不同 2. 如果使用图片,确保添加 `alt` 属性提高可访问性 3. 在移动端建议添加触摸事件支持(`@touchstart` 和 `@touchend`) > 实现效果:鼠标移入轮播区域时自动暂停,移出后继续自动播放,用户手动滑动后仍保持自动播放状态[^1][^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值