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

直接上代码

<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>

  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,根据你的要求,我给你提供一个简单的代码示例: ```html <template> <div class="notice-wrapper"> <div class="notice-content" v-for="(notice, index) in notices" :key="index" ref="notice"> <span>{{ notice }}</span> </div> </div> </template> <script> export default { data() { return { notices: ['公告1', '公告2', '公告3'], // 公告列表 intervalId: null, // 定时器ID }; }, mounted() { this.startScroll(); // 组件挂载时开始滚动 }, methods: { // 开始滚动 startScroll() { this.intervalId = setInterval(() => { const firstNotice = this.$refs.notice[0]; const height = firstNotice.offsetHeight; this.$refs.notice[0].style.marginTop = `-${height}px`; this.notices.push(this.notices.shift()); }, 2000); }, // 停止滚动 stopScroll() { clearInterval(this.intervalId); }, }, // 监听鼠标进入和离开事件 created() { this.$parent.$on('mouse-enter', this.stopScroll); this.$parent.$on('mouse-leave', this.startScroll); }, // 解绑事件 beforeDestroy() { this.$parent.$off('mouse-enter', this.stopScroll); this.$parent.$off('mouse-leave', this.startScroll); }, }; </script> <style> .notice-wrapper { height: 50px; overflow: hidden; } .notice-content { line-height: 50px; height: 50px; transition: margin-top 0.5s linear; } </style> ``` 在这个示例中,我们使用v-for指令遍历公告列表,使用CSS实现滚动效果。在组件挂载时,调用startScroll方法开始滚动,使用setInterval实现定时滚动。在鼠标进入和离开事件中,分别调用stopScroll和startScroll方法,实现鼠标移出停止滚动的效果。注意,在监听事件和解绑事件时,我们使用了$parent来访问父组件,因为鼠标事件是在父组件中触发的。 希望这个示例可以帮到你,如果还有什么不明白的地方,可以继续问我哦~
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值