Vue 自定义一个自动滚动组件

1、先看结果

样式如下

 

 2、仅仅自定义滚动组件如下

<template>
  <div class="wrapper" @mouseout="mouseOut" @mouseover="mouseOver">
    <div class="head">
      <div class="headtext">时间</div>
      <div class="headtext">预警信息</div>
      <div class="headtext"></div>
    </div>
    <div ref="moocBox" class="content">
     
      <div
        class="lineOne"
        v-for="(item, i) in batteryList"
        :key="i"
        @click="checkOne(i)"
      >
        <div class="textOne">{{ item.timeData }}</div>
        <div class="textOne">{{ item.content }}</div>
        <div class="buttonThree">
          <div class="btn" v-if="item.batteryStatus">已处理</div>
        </div>
      </div>
      <div
        class="lineOne"
        v-for="(item, i) in batteryList"
        :key="'A' + i"
        :class="{ activeBox: i == checkID }"
        @click="checkOne(i)"
      >
        <div class="textOne">{{ item.timeData }}</div>
        <div class="textOne">{{ item.content }}</div>
        <div class="buttonThree">
          <div class="btn" v-if="item.batteryStatus">已处理</div>
        </div>
      </div>
     
    </div>
  </div>
</template>

<script>
export default {
  name: "Profile",

  data() {
    return {
      animate: false,

  

      speed: 50,

      myScroll: null,

      iliHeight: 26,

      time: null,

      delay: 20,

      batteryList: [
        {
          timeData: "2021-10-02 08:45",
          content: "14号电池模组异常",
          batteryStatus: 0,
        },
        {
          timeData: "2021-10-02 08:45",
          content: "17号电池异常",
          batteryStatus: 1,
        },
        {
          timeData: "2021-10-02 08:45",
          content: "15号电池发热异常",
          batteryStatus: 1,
        },
        {
          timeData: "2021-10-02 08:45",
          content: "17号电池异常",
          batteryStatus: 1,
        },
        {
          timeData: "2021-10-02 08:45",
          content: "15号电池发热异常",
          batteryStatus: 1,
        },
        {
          timeData: "2021-10-02 08:45",
          content: "17号电池异常",
          batteryStatus: 1,
        },
        {
          timeData: "2021-10-02 08:45",
          content: "15号电池发热异常",
          batteryStatus: 1,
        },
      ],
      checkID: null,
    };
  },

  components: {},

  computed: {},

  created() {},

  mounted() {
    this.myScroll = setInterval(() => {
      this.$refs.moocBox.scrollTop++;

      this.scrollUp();
    }, this.speed);
  },
destroyed() {
    clearInterval(this.myScroll);
    // removeEventListener("scroll", this.myScroll);
  },
  methods: {
    //滚动
    checkOne(i) {
      if (this.checkID == i) {
        this.checkID = null;
      } else {
        this.checkID = i;
      }
    },
    scrollUp() {
      this.$refs.moocBox.scrollTop++;

      if (this.$refs.moocBox.scrollTop >= this.$refs.moocBox.scrollHeight / 2) {
        //判断条件是否达到临界
        this.$refs.moocBox.scrollTop = 0;
      } else {
        this.$refs.moocBox.scrollTop++;
      }
    }, // 鼠标滑过暂停滚动

    mouseOver() {
      clearInterval(this.myScroll);
    }, //鼠标移开重新滚动

    mouseOut() {
      this.myScroll = setInterval(
        () => {
          // scrollTop() 方法返回或设置匹配元素的滚动条的垂直位置
          //   ypeError: Cannot read properties of undefined (reading 'scrollTop'
          this.$refs.moocBox.scrollTop++;
          //   console.log(this.$refs.moocBox.scrollTop);
          this.scrollUp();
        },

        this.speed
      );
    },
  },

  activated() {},

  deactivated() {},

  beforeDestoryed() {
    clearInterval(this.myScroll);

    this.myScroll = null;
  },
};
</script>

<style    lang="scss"  scoped>
.wrapper {
  overflow: hidden;
  width: 356px;
  height: 103px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  .head {
    width: 100%;
    height: 32px;
    font-family: SourceHanSansSC-Regular;
    font-size: 12px;
    font-weight: normal;
    font-stretch: normal;
    line-height: 32px;
    letter-spacing: 0px;
    color: #19ffe0;
    display: flex;
    // justify-content: ;
    .headtext {
      flex: 1;
    }
  }
  .content {
    height: 180px;
    width: 310px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .lineOne {
      display: flex;
      flex-direction: row;
      height: 28px;
      width: 99%;
      .textOne {
        flex: 1;
        //   width: 94px;
        height: 28px;
        font-family: SourceHanSansSC-Regular;
        font-size: 12px;
        font-weight: normal;
        font-stretch: normal;
        line-height: 28px;
        letter-spacing: 0px;
        color: #ccfff8;
        opacity: 0.6;
      }
      .buttonThree {
        flex: 1;
        display: flex;
        justify-content: center;
        align-items: center;
        height: 28px;
        .btn {
          width: 42px;
          height: 18px;
          background-image: linear-gradient(0deg, #19ffe0 0%, #00b29b 100%);
          border-radius: 4px;
          font-family: SourceHanSansSC-Bold;
          font-size: 10px;
          font-weight: normal;
          font-stretch: normal;
          // line-height: 32px;
          letter-spacing: 0px;
          color: #000d0b;
        }
      }
    }
  }
}

.activeBox {
  //   background-color: #ffb630;
  border: solid 0.5px #ffb630;
  background-image: linear-gradient(
    0deg,
    rgba(254, 183, 48, 0.4) 0%,
    rgba(0, 13, 11, 0.4) 100%
  );
}

table  {
  border-collapse: collapse;
  border-spacing: 0;
  table-layout: fixed;
  /* background-color: rgb(2,  25,  80); */
  width: 100%;
  /* color: #fff; */
}
table tr  {
  //   transition: all 2s ease .3s;
}
.marquee_top  {
  //   transition: all 0.5s ease-in-out;
  margin-top: -26px;
  background-color: blue;
}

.title  {
  /* height: 40px;
  line-height: 40px;
  text-align: center; */
  width: 24px;
  height: 32px;
  font-family: SourceHanSansSC-Regular;
  font-size: 12px;
  font-weight: normal;
  font-stretch: normal;
  line-height: 32px;
  letter-spacing: 1px;
  color: #19ffe0;
}

th {
  padding: 5px 10px;
  width: 25%;
}

td {
  padding: 4px 10px;
  width: 25%;
}

table th {
  text-align: center;
}

table tr td  {
  width: 25%;
  text-align: center;
}
.rollData  {
  font-size: 16px;
}
</style>

3、讲解如下

1、mounted中加载时候自动执行一个自定义定时器myScroll ,不断调用scrollUp

mounted() {
    this.myScroll = setInterval(() => {
      this.$refs.moocBox.scrollTop++;

      this.scrollUp();
    }, this.speed);
  },

2、scrollUp 判断是否高度达到临界值,scrollTop() 方法返回或设置匹配元素的滚动条的垂直位置

scrollUp() {
      this.$refs.moocBox.scrollTop++;

      if (this.$refs.moocBox.scrollTop >= this.$refs.moocBox.scrollHeight / 2) {
        //判断条件是否达到临界
        this.$refs.moocBox.scrollTop = 0;
      } else {
        this.$refs.moocBox.scrollTop++;
      }
    }, // 鼠标滑过暂停滚动

3、鼠标移入时候

  mouseOut() {
      this.myScroll = setInterval(
        () => {
          // scrollTop() 方法返回或设置匹配元素的滚动条的垂直位置
          //   ypeError: Cannot read properties of undefined (reading 'scrollTop'
          this.$refs.moocBox.scrollTop++;
          //   console.log(this.$refs.moocBox.scrollTop);
          this.scrollUp();
        },

        this.speed
      );
    },

4、鼠标移出时候调用,销毁定时器

 mouseOver() {
      clearInterval(this.myScroll);
    }, //鼠标移开重新滚动

5、销毁定时器方法

beforeDestoryed() {
    clearInterval(this.myScroll);

    this.myScroll = null;
  },

6.切换页面销毁定时器

destroyed() {
    clearInterval(this.myScroll);
    // removeEventListener("scroll", this.myScroll);
  },

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值