vue封装堆叠式进度条

效果图
在这里插入图片描述
代码

App.vue

<processBar :processValue="processValue1"/>

<script>
import processBar from './components/ProcessBar'
export default {
  name: 'App',
  components: {
    processBar
  },
  data () {
    return {
      processValue1: [
        {
          name: '乙烯',
          color: '#3EA4F7',
          data: 30494
        },
        {
          name: '丁二烯',
          color: '#AAD9F7',
          data: 3585
        },
        {
          name: '化工苯',
          color: '#23628D',
          data: 7615
        },
        {
          name: '对二甲苯',
          color: '#5EBAF8',
          data: 12881
        },
        {
          name: '苯乙烯',
          color: '#2D7AB8',
          data: 6696
        }
      ],
    }
  },
  methods: {}
}
</script>

在组件中ProcessBar中

<template>
  <div>
    <div id="ProcessBar" class="pB_Container">
      <!--   封装遍历-->
      <div v-for="(item,index) in processArray" :key="index"
           :style="{ width:item.data+'%',backgroundColor:item.color,paddingLeft:10+'px'}"></div>
    </div>
    <div class="pB_Container_bottom">
      <ul>
        <li v-for="(item,index) in processArray" :key="index">
          <span style="display:inline-block;width:10px;height: 10px;" :style="{backgroundColor: item.color}"></span>
          <span>{{item.name}}</span>
          <span class="data">{{item.data}}</span>
        </li>
        <li></li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ProcessBar',
  props: {
    processValue: {
      type: Array,
      default: [0],
    },
  },
  data () {
    return {
      dataSum: 0,
      processArray: []
    }
  },
  components: {},
  created () {
    this.processValue.forEach(item => {
      this.dataSum += item.data
    })
    this.processArray = this.processValue.map(items => {
      return {data: Math.round(((items.data / this.dataSum) * 100)), color: items.color, name: items.name}
    })
    console.log(this.processArray)
  },
  methods: {
    getProcessDesc (data) {
      return data == 0 ? ' ' : data + '%'
    },
  },
}
</script>
<style scoped>
  .pB_Container {
    width: 100%;
    background-color: #f5f5f5;
    height: 10px;
    display: inline-flex;
    line-height: 10px;
    overflow: hidden;
    color: white;
    box-shadow: inset 0 2px 2px rgba(0, 0, 0, 0.1);
  }

  .pB_Container div {
    float: left;
    /*border-radius: 8px;*/
  }

  .pB_Container_bottom ul {
    list-style: none;
    text-align: left;
  }
  .pB_Container_bottom ul li{
    float: left;
    display: inline-block;
    width:33%
  }
  .data{
    display: inline-block;
    float:right;padding-right: 10%;
    color: #FFDE00;
    font-weight: 700;
    font-size: 16px;
  }
</style>

最终实现效果
在这里插入图片描述

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
可以使用Vue组件的方封装一个进度条,具体实现如下: ```html <template> <div class="progress-bar"> <div class="progress" :style="{ width: percentage + '%' }"></div> </div> </template> <script> export default { name: 'ProgressBar', props: { percentage: { type: Number, required: true, default: 0 } } } </script> <style> .progress-bar { width: 100%; height: 10px; background-color: #e0e0e0; border-radius: 5px; overflow: hidden; } .progress { height: 100%; background-color: #4caf50; transition: width 0.3s ease-in-out; } </style> ``` 在上述代码中,我们定义了一个名为`ProgressBar`的Vue组件,其中包含一个`percentage`属性,表示进度条的进度百分比。该组件使用CSS样实现进度条的效果,具体实现如下: - `.progress-bar`:表示进度条的外层容器,包含背景色和边框样。 - `.progress`:表示进度条的进度部分,包含进度条颜色和过渡效果。 在组件的模板中,我们使用`percentage`属性来计算进度条的宽度,并将其绑定到`.progress`样的`width`属性上。这样,当`percentage`值发生变化时,进度条的宽度也会发生相应的变化。 使用该进度条组件的方如下: ```html <template> <div> <progress-bar :percentage="progress"></progress-bar> <button @click="increaseProgress">增加进度</button> </div> </template> <script> import ProgressBar from './ProgressBar.vue' export default { name: 'App', components: { ProgressBar }, data() { return { progress: 50 } }, methods: { increaseProgress() { this.progress += 10 } } } </script> ``` 在上述代码中,我们在父组件中引入了`ProgressBar`组件,并将`progress`属性绑定到子组件的`percentage`属性上。同时,在父组件中定义了一个按钮,点击按钮可以增加进度条的进度。 这样,我们就可以通过Vue组件来封装一个进度条,并在其他组件中方便地重复使用。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值