vue 实现列表自动轮播,并且鼠标移入停止列表,移出继续滚动

这段代码展示了如何使用 Vue.js 创建一个无缝滚动效果。通过监听 `mouseenter` 和 `mouseleave` 事件来控制定时器,实现在鼠标悬停时停止滚动,离开时恢复。主要涉及 `scroll` 事件处理、DOM 操作以及定时器的使用,创建了两个相同内容的列表进行无缝滚动。

直接上代码

<template>
  <div>

    <div
      id="box"
      @mouseenter="mouseenterEvent()"
      @mouseleave="mouseleaveEvent(scrollTime)"
    >
      <ul id="listbox1">
        <li v-for="(item, index) in list" :key="index">{{ item }}</li>
      </ul>
      <ul id="listbox2">
  
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
      timer: null,
      scrollTime: 20,
    };
  },
  methods: {
    scroll(t) {
      var ul1 = document.getElementById("listbox1");

      var ul2 = document.getElementById("listbox2");

      var ulbox = document.getElementById("box");

      ul2.innerHTML = ul1.innerHTML;

      ulbox.scrollTop = 0; // 开始无滚动时设为0

      this.timer = setInterval(this.rollStart, t);
    },
    // 开启滚动
    rollStart() {
      // 上面声明的DOM对象为局部对象需要再次声明
      // 无缝轮播需要两个相同内容的盒子
      var ul1 = document.getElementById("listbox1");

      // var ul2 = document.getElementById("listbox2");

      var ulbox = document.getElementById("box");

      // 正常滚动不断给scrollTop的值+1,当滚动高度大于列表内容高度时恢复为0

      if (ulbox.scrollTop >= ul1.scrollHeight) {
        ulbox.scrollTop = 0;
      } else {
        ulbox.scrollTop++;
      }
    },
    // 鼠标移入关闭定时器
    mouseenterEvent() {
      if (this.timer) {
        clearInterval(this.timer);
        this.timer = null;
      }
      // // 只显示一个盒子的内容
      // document.getElementById("listbox2").style.display = "none";
    },
    // 鼠标移出重新调用定时器
    mouseleaveEvent(t) {
      console.log(1);
      if (!this.timer) {
        this.timer = setInterval(this.rollStart, t);
      }
      //  document.getElementById("listbox2").style.display = "block";
    },
  },
  mounted() {
    //开启滚动
    this.scroll(this.scrollTime);
  },
  destroyed() {
    // 页面销毁清除定时器
    if (this.timer) {
      clearInterval(this.timer);
      this.timer = null;
    }
  },
};
</script>

<style lang="less" scoped>
#box {
  width: 500px;
  height: 200px;
  border: 1px solid #000;
  //溢出隐藏
  overflow: hidden;
  &:hover {
    // 实现Y轴可滚动
    overflow-y: scroll;
  }
  &::-webkit-scrollbar {
    width: 0px;
  }
}
#listbox1 {
  width: 500px;
  height: 400px;
  border: 1px solid red;
  box-sizing: border-box;

  li {
    height: 40px;
    border: 1px solid blue;
    box-sizing: border-box;
  }
}
#listbox2 {
  width: 500px;
  height: 400px;
  border: 1px solid red;
  box-sizing: border-box;

  li {
    height: 40px;
    border: 1px solid blue;
    box-sizing: border-box;
  }
}
</style>

实现 Element UI 的轮播图组件(Carousel)在鼠标滚动到底部时停止轮播的功能,可以通过监听窗口的滚动事件并判断是否滚动到了轮播图容器的底部来完成。当检测到滚动到底部时,可以使用 `el-carousel` 提供的 API 来暂停自动播放。 ### 实现步骤 1. **HTML 结构**:确保你的 `el-carousel` 组件有一个明确的容器,并为其设置一个唯一的 `ref` 以便于在 JavaScript 中访问。 2. **JavaScript 逻辑**:通过 `window.addEventListener('scroll', ...)` 监听滚动事件,并计算当前轮播图组件的位置和滚动状态。 3. **控制 Carousel 自动播放**:使用 `el-carousel` 提供的 `stopPlay` 和 `startPlay` 方法来控制轮播自动播放。 ### 示例代码 ```vue <template> <div ref="carouselContainer" @mouseenter="pauseCarousel" @mouseleave="resumeCarousel"> <el-carousel :autoplay="true" ref="carousel"> <el-carousel-item v-for="(item, index) in items" :key="index"> <h3>{{ item }}</h3> </el-carousel-item> </el-carousel> </div> </template> <script> export default { data() { return { items: ['Slide 1', 'Slide 2', 'Slide 3', 'Slide 4'], isPaused: false, }; }, mounted() { window.addEventListener('scroll', this.handleScroll); }, beforeDestroy() { window.removeEventListener('scroll', this.handleScroll); }, methods: { handleScroll() { const container = this.$refs.carouselContainer; if (!container) return; const rect = container.getBoundingClientRect(); const windowHeight = window.innerHeight || document.documentElement.clientHeight; const scrollBottom = rect.top + rect.height <= windowHeight; if (scrollBottom && !this.isPaused) { this.pauseCarousel(); } else if (!scrollBottom && this.isPaused) { this.resumeCarousel(); } }, pauseCarousel() { this.$refs.carousel.stopPlay(); this.isPaused = true; }, resumeCarousel() { this.$refs.carousel.startPlay(); this.isPaused = false; }, }, }; </script> <style scoped> .el-carousel__item h3 { color: #475669; font-size: 18px; line-height: 300px; margin: 0; text-align: center; } </style> ``` ### 解释 - **`handleScroll` 方法**:此方法会检查轮播图组件是否已经滚动到了视口的底部。它通过 `getBoundingClientRect()` 获取元素的位置信息,并结合窗口的高度来判断是否滚动到底部。 - **`pauseCarousel` 和 `resumeCarousel` 方法**:这些方法分别调用了 `el-carousel` 的 `stopPlay` 和 `startPlay` 方法,用于控制轮播自动播放[^1]。 - **`@mouseenter` 和 `@mouseleave` 事件**:这两个事件用于在用户将鼠标移入移出轮播图区域时暂停或恢复轮播。 ### 注意事项 - 确保为轮播图容器添加了足够的样式以正确显示内容。 - 在组件销毁前移除滚动事件监听器,避免内存泄漏。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值