vue2.X 实现动态进度条效果

文章描述了一个使用Vue.js编写的动态百分比进度条组件,通过监听数据变化计算并更新进度,同时展示了CSS样式和动画效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<template lang="pug">
      .line
        .proportion(:style="`width: ${percent}%;`")
          img(
            src="../../../../assets/images/xxx/xxx/patrol_icon.png",
            :style="`left: ${percent + 9}%;`"
          )
</template>
<script>
export default {
  data() {
    return {
      percent: 0,
      startValue: 0, // 初始值
      endValue: 0, // 结束值
      duration: 1, // 动画时间
      playAnimation: null,
    };
  },
  watch: {},
  mounted() {
  },
  created() {
    GetData({}).then(res => {
      if(res&&res.length) {
        let data = res[0]
        if(data.total) { // 除法:numDiv 保留两位小数:filterToFixed
          this.percent = this.filterToFixed((this.numDiv(data.finishData, data.total) || 0)*100, 2)
          this.endValue = this.percent
        }
        this.progress();
  },
  methods: {
    progress() {
      const that = this;
      let progress = this.startValue;
      const endValue = this.endValue;
      let time = (this.duration * 1000) / (endValue - progress); // 根据传入时间计算执行时间
      if (progress >= endValue) {
        progress = endValue;
      }
      this.playAnimation = setInterval(function () {
        progress++;
        if (progress >= endValue) {
          progress = endValue;
          clearInterval(this.playAnimation);
        }
        that.percent = progress;
      }, time);
    },
  },
  beforeDestroy() {
    clearInterval(this.playAnimation);
    this.playAnimation = null;
  },
};
</script>
<style scoped lang="less">
.patrolClassification {
  width: 100%;
  height: 100%;
  position: relative;
  .progress-warps {
    width: 52%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .line {
      position: relative;
      width: 202px;
      height: 14px;
      background: rgba(255, 255, 255, 0.1);
      border: 1px solid #00c4ff;
      img {
        width: 32px;
        position: absolute;
        top: -48px;
        // left: -32px;
      }
      .proportion {
        background: linear-gradient(90deg, #00c4ff 0%, #00ffe6 100%);
        width: 137px;
        height: 8px;
        position: absolute;
        top: 2px;
        left: 2px;
      }
    }
  }
}
</style>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值