vue 分段进度条组件

分段进度条组件

请添加图片描述

非组件封装的 demo

组件封装代码

./components/tick-progress.vue

<template>
  <div class="container">
    <div class="progress-container" :style="styleObj">
      <div class="progress" :style="{width: precent+'%'}"></div>
      <div class="progress-bg"></div>
      <div class="progress-tick-container">
        <div class="progress-tick" v-for="(item, index) in (new Array(tickNum))" :key="'tick' + index"></div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    // 进度条占值百分比
    precent: {
      type: Number,
      // default: 0
      default: 30
    },
    height: {
      type: Number,
      default: 16
    },
    // 间距
    spacing: {
      type: String,
      default: "2px"
    },
    // 进度条整体背景色
    wholeBgColor: {
      type: String,
      default: "#03091d"
    },
    // 进度条背景色
    bgColor: {
      type: String,
      default: "#0a1c45"
    },
    // 进度条有效值颜色
    activeColor: {
      type: String,
      default: "#1f8df2"
    }
  },
  data() {
    return {
      tickNum: 10,
      styleObj: {}
    };
  },
  mounted() {
    this.styleObj = {
      height: this.height / 100 + "rem",
      // height: this.height + "px",
      "--spacing": this.spacing,
      "--whole-bg-color": this.wholeBgColor,
      "--bg-color": this.bgColor,
      "--active-color": this.activeColor
    };
  },
}
</script>

<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
}
#app {
  width: 100vw;
  height: 100vh;
  background-color: #010f3f33;
  display: flex;
  align-items: center;
  justify-content: center;
}
html {
  font-size: 100px;
}
body {
  font-size: 16px;
}

div.container {
  width: 400px;
  // height: 16px;
}
.progress-container {
  $progress-spacing: var(--spacing, 2px);
  $progress-whole-bg-color: var(--whole-bg-color, #03091d);
  $progress-bg-color: var(--whole-bg-color, #0a1c45);
  $progress-active-color: var(--active-color, #1f8df2);
  width: 100%;
  // width: 400px;
  background-color: $progress-whole-bg-color;
  border: 1px solid $progress-bg-color;
  position: relative;
  padding: $progress-spacing;
  // box-sizing: border-box;
  .progress {
    position: absolute;
    left: $progress-spacing;
    top: $progress-spacing;
    max-width: calc(100% - (#{$progress-spacing} * 2));
    height: calc(100% - (#{$progress-spacing} * 2));
    background-color: $progress-active-color;
    z-index: 9;
  }
  .progress-bg {
    background-color: $progress-bg-color;
    position: absolute;
    left: $progress-spacing;
    top: $progress-spacing;
    width: calc(100% - (#{$progress-spacing} * 2));
    height: calc(100% - (#{$progress-spacing} * 2));
    z-index: 1;
  }
  .progress-tick-container {
    width: calc(100% - (#{$progress-spacing} * 2));
    height: calc(100% - (#{$progress-spacing} * 2));
    position: absolute;
    left: $progress-spacing;
    top: $progress-spacing;
    display: flex;
    justify-content: space-evenly;
    .progress-tick {
      width: calc(#{$progress-spacing} * 2);
      height: 100%;
      background-color: $progress-whole-bg-color;
      z-index: 99;
    }
  }
}
</style>

使用

<template>
	<TickProgress :precent="precent"></TickProgress>
	<TickProgress :precent="62" :tickNum="5"></TickProgress>
	<TickProgress :precent="62" :activeColor="rgb(252,198,44)"></TickProgress>
</template>

<script>
import TickProgress from "./components/tick-progress.vue"
export default {
  components: { TickProgress },
  data () {
    return {
      precent: 30
    }
  }
}
</script>

参考博客:

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
下面是一个简单的分段进度条组件实现代码: ``` <template> <div class="segment-progress-bar"> <div v-for="(item, index) in segments" :key="index" :class="getSegmentClass(item)"> <span class="label">{{ item }}%</span> </div> </div> </template> <script> export default { name: 'SegmentProgressBar', props: { segments: { type: Array, required: true }, total: { type: Number, required: true } }, methods: { getSegmentClass(item) { let cls = 'segment'; if (item === 100) { cls += ' completed'; } else if (item > 0) { cls += ' active'; } return cls; } } } </script> <style> .segment-progress-bar { display: flex; justify-content: space-between; height: 20px; border: 1px solid #ccc; } .segment { height: 100%; flex-grow: 1; border-right: 1px solid #ccc; position: relative; } .segment:last-child { border-right: none; } .segment.active { background-color: #fcc; } .segment.completed { background-color: #cfc; } .label { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } </style> ``` 在上面的代码中,我们首先定义了一个名为SegmentProgressBar的Vue组件,包含两个props:segments和total,分别表示进度条的完成情况和总数。在template中,我们使用v-for指令遍历segments数组,为每个进度条渲染一个div元素,并根据其完成情况添加不同的class。在script中,我们定义了一个getSegmentClass方法,根据进度条的完成情况返回相应的class名称。在style中,我们定义了进度条和标签的样。 在父组件中,我们可以这样使用SegmentProgressBar组件: ``` <template> <div> <h3>Segment Progress Bar</h3> <segment-progress-bar :segments="segments" :total="total"></segment-progress-bar> </div> </template> <script> import SegmentProgressBar from '@/components/SegmentProgressBar.vue'; export default { name: 'App', components: { SegmentProgressBar }, data() { return { segments: [20, 60, 80, 100], total: 4 } } } </script> ``` 在父组件中,我们引入了SegmentProgressBar组件,并传递了segments和total属性。segments属性是一个数组,包含四个进度条的完成情况,total属性是数字4,表示进度条总数。在浏览器中打开页面,就可以看到一个简单的分段进度条组件了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

RealizeInnerSelf丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值