el-table使用requestAnimationFrame自动滚动

我要冲啦个人网站建设2023-12-26web前端

vue文件中 el-table使用requestAnimationFrame实现自动滚动

相比定时器更加丝滑

data() {
  return {
     rAFid: "",
      }
},
methods:{
    myscroll(el) {
      el.scrollTop += 1;
      if (el.clientHeight + el.scrollTop >= el.scrollHeight) {
        el.scrollTo({
          top: 0,
        });
      }
      this.rAFid = requestAnimationFrame(this.myscroll.bind(null, el));
    },
},
mounted() {
      var me = this;
      const el = this.$refs.projectTable.$refs.bodyWrapper; //表格
      me.rAFid = window.requestAnimationFrame(me.myscroll.bind(null, el));
      el.addEventListener("mouseenter", () => { //鼠标悬停停止
        window.cancelAnimationFrame(me.rAFid);
      });
      el.addEventListener("mouseleave", () => { //鼠标移出开始
        me.rAFid = window.requestAnimationFrame(me.myscroll.bind(null, el));
      });
}

文章关键词
el-table
requestAnimationFrame自动滚动