vue实现多个进度条循环自增长动效,定时器实现自动变化的进度条组件

简单概述下实现效果,是一个排行榜多个进度条需要一个自增长的动效,通过循环进度条组件定时器实现自增长。

效果如下:

代码如下:

<template>
  <div class="line">
    <div
      class="proportion"
      :style="`width: ${percent}%;`"
    ></div>
  </div>
</template>

<script>
export default {
  name: 'Progress', //进度条
  props: {
    //初始值
    startValue: {
      type: Number,
      default: 0,
    },
    //结束值
    endValue: {
      type: Number,
      default: 100,
    },
    //动画时间
    duration: {
      type: Number,
      default: 1,
    },
  },
  data() {
    return {
      percent: 0,
    }
  },
  mounted() {
    this.percent = this.startValue >= this.endValue ? this.endValue : this.startValue
    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
      }
      let clearInt = setInterval(function(){
        progress++
        if(progress >= endValue){
          progress = endValue
          clearInterval(clearInt);
        }
        that.percent = progress
      },time)
    },
  },
}
</script>

<style lang="less" scoped>
.line {
  width: 100%;
  height: 100%;
  border-radius: 10px;
  background: #f5f5f5;
  position: relative;
  .proportion {
    border-radius: 10px;
    background: #00d0a2;
    height: 100%;
    position: absolute;
  }
}
</style>

引入:

<div v-for="item in list" :key="item.key">
    <Progress
        style="height: 20px;"
        :startValue="0"
        :endValue="item.max"
        :duration="1"
    ></Progress>
</div>

注意:!!!

在for循环进度条组件时 需要把key设置成随机数,不然重新请求接口时key值是不变的,vue的diff算法是不会更新这个子组件也就不会触发子组件的生命周期,就无法重新执行动效了。

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过计算属性和绑定样式实现圆环进度条根据参数变化。 首先,在 Vue3 中创建一个组件,将参数作为组件的 props 传入。然后,使用计算属性计算出进度条的样式,并将样式绑定到对应的 HTML 元素上。最后,使用 CSS 实现圆环的效果。 下面是一个示例代码: ```vue <template> <div class="progress-ring"> <div class="progress-bar" :style="progressStyle"></div> <div class="progress-label">{{ progress }}%</div> </div> </template> <script> export default { props: { progress: { type: Number, required: true, }, }, computed: { // 计算进度条的样式 progressStyle() { const progress = this.progress <= 100 ? this.progress : 100; const rotation = `rotate(${(progress / 100) * 360}deg)`; return { transform: rotation, }; }, }, }; </script> <style scoped> .progress-ring { position: relative; width: 100px; height: 100px; } .progress-bar { position: absolute; width: 100%; height: 100%; border-radius: 50%; clip: rect(0, 50px, 100px, 0); background-color: #f2f2f2; transform: rotate(0deg); z-index: -1; } .progress-bar::before { content: ""; position: absolute; width: 100%; height: 100%; border-radius: 50%; clip: rect(0, 50px, 100px, 0); background-color: #2196f3; transform-origin: center center; } .progress-label { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 24px; font-weight: bold; color: #2196f3; } </style> ``` 在上面的代码中,我们定义了一个名为 `progress-ring` 的容器,容器中包含一个圆环进度条和一个显示进度百分比的标签。通过计算属性 `progressStyle` 计算出进度条的样式,并将样式绑定到 `progress-bar` 元素上。最后,使用 CSS 实现圆环的效果。 你可以将这个组件作为子组件嵌入到父组件中,在父组件中传入进度参数即可实现根据参数变化的圆环进度条
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值