Vue2.x 不封闭圆形进度条设计说明学习

引言

在Web应用中,进度条是一种常见的UI元素,用于向用户展示任务的完成情况。圆形进度条因其独特的视觉效果而受到欢迎,特别是在需要显示百分比完成度时。本文将详细介绍如何使用Vue.js 2.x创建一个不封闭的圆形进度条组件,并通过多个示例来探讨其不同的使用场景和实现技巧。

基本概念与作用

定义

  • 圆形进度条:一种UI组件,通常用于显示完成进度,形状为圆环或部分圆环。
  • 不封闭:指进度条在达到100%时也不会完全闭合,而是留有一定的缺口。

作用

  • 提供直观的视觉反馈。
  • 显示任务的完成进度。
  • 支持多种样式定制。

技术栈

  • Vue.js 2.x:用于构建组件。
  • SVG:用于绘制圆形进度条。
  • CSS3:用于样式定制。
  • JavaScript:用于动态更新进度。

示例一:基础圆形进度条组件

首先,我们将创建一个简单的圆形进度条组件,该组件接受value属性来设置进度条的当前值。

<template>
  <div class="circular-progress">
    <svg :width="size" :height="size">
      <circle
        cx="50%"
        cy="50%"
        :r="radius"
        fill="transparent"
        stroke="#ccc"
        stroke-width="10"
      />
      <circle
        cx="50%"
        cy="50%"
        :r="radius"
        fill="transparent"
        :stroke="progressColor"
        :stroke-width="10"
        :stroke-dasharray="circumference"
        :stroke-dashoffset="dashOffset"
      />
    </svg>
  </div>
</template>

<script>
export default {
  props: {
    value: {
      type: Number,
      default: 0
    },
    size: {
      type: Number,
      default: 100
    },
    progressColor: {
      type: String,
      default: '#009688'
    }
  },
  computed: {
    radius() {
      return (this.size / 2) - 10;
    },
    circumference() {
      return 2 * Math.PI * this.radius;
    },
    dashOffset() {
      const percent = 100 - this.value;
      return (percent / 100) * this.circumference;
    }
  }
}
</script>

<style scoped>
.circular-progress {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>

说明

  • 这个组件使用两个<circle>元素,一个作为背景,另一个作为进度条。
  • value属性用于设置进度百分比。
  • size属性定义了SVG的尺寸。
  • 计算属性radiuscircumferencedashOffset用于计算进度条的半径、周长以及偏移量。

示例二:动态更新进度

接下来,我们添加一些交互逻辑,使进度条可以响应用户的输入。

<template>
  <div>
    <input v-model.number="value" type="range" min="0" max="100" step="1" />
    <component :is="circularProgress" :value="value" :size="size" :progress-color="progressColor" />
  </div>
</template>

<script>
import CircularProgress from './CircularProgress.vue';

export default {
  components: {
    CircularProgress
  },
  data() {
    return {
      value: 0,
      size: 100,
      progressColor: '#009688'
    };
  }
}
</script>

说明

  • 添加了一个<input>元素,允许用户通过滑动来改变进度。
  • 使用v-model.number来双向绑定value属性。
  • circularProgress组件接收valuesizeprogressColor属性。

示例三:带文字标签的进度条

在这个例子中,我们将在进度条内部添加文字标签,显示当前的百分比值。

<template>
  <div class="circular-progress">
    <svg :width="size" :height="size">
      <circle
        cx="50%"
        cy="50%"
        :r="radius"
        fill="transparent"
        stroke="#ccc"
        stroke-width="10"
      />
      <circle
        cx="50%"
        cy="50%"
        :r="radius"
        fill="transparent"
        :stroke="progressColor"
        :stroke-width="10"
        :stroke-dasharray="circumference"
        :stroke-dashoffset="dashOffset"
      />
      <text x="50%" y="50%" text-anchor="middle" dominant-baseline="central" font-size="20">{{ value }}%</text>
    </svg>
  </div>
</template>

<script>
export default {
  props: {
    value: {
      type: Number,
      default: 0
    },
    size: {
      type: Number,
      default: 100
    },
    progressColor: {
      type: String,
      default: '#009688'
    }
  },
  computed: {
    radius() {
      return (this.size / 2) - 10;
    },
    circumference() {
      return 2 * Math.PI * this.radius;
    },
    dashOffset() {
      const percent = 100 - this.value;
      return (percent / 100) * this.circumference;
    }
  }
}
</script>

<style scoped>
.circular-progress {
  display: flex;
  justify-content: center;
  align-items: center;
}

.circular-progress text {
  fill: #000;
}
</style>

说明

  • 在SVG中添加了一个<text>元素,用于显示进度条的值。
  • 使用dominant-baselinetext-anchor属性来居中文本。

示例四:带动画效果的进度条

为了增强用户体验,我们可以添加一个简单的动画效果,使得进度条在变化时更加平滑。

<template>
  <div class="circular-progress">
    <svg :width="size" :height="size">
      <circle
        cx="50%"
        cy="50%"
        :r="radius"
        fill="transparent"
        stroke="#ccc"
        stroke-width="10"
      />
      <circle
        cx="50%"
        cy="50%"
        :r="radius"
        fill="transparent"
        :stroke="progressColor"
        :stroke-width="10"
        :stroke-dasharray="circumference"
        :stroke-dashoffset="dashOffset"
        :class="{ animated: isAnimated }"
      />
    </svg>
  </div>
</template>

<script>
export default {
  props: {
    value: {
      type: Number,
      default: 0
    },
    size: {
      type: Number,
      default: 100
    },
    progressColor: {
      type: String,
      default: '#009688'
    }
  },
  data() {
    return {
      isAnimated: false
    };
  },
  computed: {
    radius() {
      return (this.size / 2) - 10;
    },
    circumference() {
      return 2 * Math.PI * this.radius;
    },
    dashOffset() {
      const percent = 100 - this.value;
      return (percent / 100) * this.circumference;
    }
  },
  watch: {
    value(newVal) {
      if (newVal !== 0) {
        this.isAnimated = true;
        setTimeout(() => {
          this.isAnimated = false;
        }, 1000);
      }
    }
  }
}
</script>

<style scoped>
.circular-progress {
  display: flex;
  justify-content: center;
  align-items: center;
}

.circular-progress circle.animated {
  transition: stroke-dashoffset 0.5s ease-in-out;
}
</style>

说明

  • 使用watch来监听value的变化。
  • value发生变化时,添加animated类来触发CSS过渡效果。

示例五:可配置的进度条

最后,我们将创建一个更复杂的进度条组件,允许用户配置更多的选项,如起点位置、方向等。

<template>
  <div class="circular-progress">
    <svg :width="size" :height="size">
      <circle
        cx="50%"
        cy="50%"
        :r="radius"
        fill="transparent"
        stroke="#ccc"
        stroke-width="10"
      />
      <circle
        cx="50%"
        cy="50%"
        :r="radius"
        fill="transparent"
        :stroke="progressColor"
        :stroke-width="10"
        :stroke-dasharray="circumference"
        :stroke-dashoffset="dashOffset"
        :transform="transform"
      />
    </svg>
  </div>
</template>

<script>
export default {
  props: {
    value: {
      type: Number,
      default: 0
    },
    size: {
      type: Number,
      default: 100
    },
    progressColor: {
      type: String,
      default: '#009688'
    },
    startAngle: {
      type: Number,
      default: 0
    },
    clockwise: {
      type: Boolean,
      default: true
    }
  },
  computed: {
    radius() {
      return (this.size / 2) - 10;
    },
    circumference() {
      return 2 * Math.PI * this.radius;
    },
    dashOffset() {
      const percent = 100 - this.value;
      return (percent / 100) * this.circumference;
    },
    transform() {
      const angle = (this.startAngle * Math.PI) / 180;
      const rotate = `rotate(${angle} 50% 50%)`;
      const flip = this.clockwise ? '' : 'scale(-1, 1)';
      return `${rotate} ${flip}`;
    }
  }
}
</script>

<style scoped>
.circular-progress {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>

说明

  • 新增startAngleclockwise属性,分别控制进度条的起始角度和旋转方向。
  • 使用transform属性来调整进度条的位置和方向。

实际工作中的一些技巧

  1. 性能优化:对于大量动态数据的进度条,考虑使用requestAnimationFrame或者Vue.nextTick来更新DOM,避免不必要的重绘。
  2. 自定义样式:利用CSS变量和SCSS/SASS来创建可定制的主题,提高复用性和维护性。
  3. 国际化支持:如果进度条需要显示在多语言环境中,可以考虑使用Vue的国际化插件来支持多语言的文本显示。
  4. 无障碍性:确保进度条组件符合WCAG标准,比如通过aria-labelaria-valuenow属性提供辅助信息。

以上就是关于如何使用Vue.js 2.x创建不封闭圆形进度条的基本介绍和实现方法。希望这些示例和技巧可以帮助你在项目中更好地使用这一UI元素。


欢迎来到我的博客,很高兴能够在这里和您见面!希望您在这里可以感受到一份轻松愉快的氛围,不仅可以获得有趣的内容和知识,也可以畅所欲言、分享您的想法和见解。


推荐:DTcode7的博客首页。
一个做过前端开发的产品经理,经历过睿智产品的折磨导致脱发之后,励志要翻身农奴把歌唱,一边打入敌人内部一边持续提升自己,为我们广大开发同胞谋福祉,坚决抵制睿智产品折磨我们码农兄弟!


专栏系列(点击解锁)学习路线(点击解锁)知识定位
《微信小程序相关博客》持续更新中~结合微信官方原生框架、uniapp等小程序框架,记录请求、封装、tabbar、UI组件的学习记录和使用技巧等
《AIGC相关博客》持续更新中~AIGC、AI生产力工具的介绍,例如stable diffusion这种的AI绘画工具安装、使用、技巧等总结
《HTML网站开发相关》《前端基础入门三大核心之html相关博客》前端基础入门三大核心之html板块的内容,入坑前端或者辅助学习的必看知识
《前端基础入门三大核心之JS相关博客》前端JS是JavaScript语言在网页开发中的应用,负责实现交互效果和动态内容。它与HTML和CSS并称前端三剑客,共同构建用户界面。
通过操作DOM元素、响应事件、发起网络请求等,JS使页面能够响应用户行为,实现数据动态展示和页面流畅跳转,是现代Web开发的核心
《前端基础入门三大核心之CSS相关博客》介绍前端开发中遇到的CSS疑问和各种奇妙的CSS语法,同时收集精美的CSS效果代码,用来丰富你的web网页
《canvas绘图相关博客》Canvas是HTML5中用于绘制图形的元素,通过JavaScript及其提供的绘图API,开发者可以在网页上绘制出各种复杂的图形、动画和图像效果。Canvas提供了高度的灵活性和控制力,使得前端绘图技术更加丰富和多样化
《Vue实战相关博客》持续更新中~详细总结了常用UI库elementUI的使用技巧以及Vue的学习之旅
《python相关博客》持续更新中~Python,简洁易学的编程语言,强大到足以应对各种应用场景,是编程新手的理想选择,也是专业人士的得力工具
《sql数据库相关博客》持续更新中~SQL数据库:高效管理数据的利器,学会SQL,轻松驾驭结构化数据,解锁数据分析与挖掘的无限可能
《算法系列相关博客》持续更新中~算法与数据结构学习总结,通过JS来编写处理复杂有趣的算法问题,提升你的技术思维
《IT信息技术相关博客》持续更新中~作为信息化人员所需要掌握的底层技术,涉及软件开发、网络建设、系统维护等领域的知识
《信息化人员基础技能知识相关博客》无论你是开发、产品、实施、经理,只要是从事信息化相关行业的人员,都应该掌握这些信息化的基础知识,可以不精通但是一定要了解,避免日常工作中贻笑大方
《信息化技能面试宝典相关博客》涉及信息化相关工作基础知识和面试技巧,提升自我能力与面试通过率,扩展知识面
《前端开发习惯与小技巧相关博客》持续更新中~罗列常用的开发工具使用技巧,如 Vscode快捷键操作、Git、CMD、游览器控制台等
《photoshop相关博客》持续更新中~基础的PS学习记录,含括PPI与DPI、物理像素dp、逻辑像素dip、矢量图和位图以及帧动画等的学习总结
日常开发&办公&生产【实用工具】分享相关博客》持续更新中~分享介绍各种开发中、工作中、个人生产以及学习上的工具,丰富阅历,给大家提供处理事情的更多角度,学习了解更多的便利工具,如Fiddler抓包、办公快捷键、虚拟机VMware等工具

吾辈才疏学浅,摹写之作,恐有瑕疵。望诸君海涵赐教。望轻喷,嘤嘤嘤
非常期待和您一起在这个小小的网络世界里共同探索、学习和成长。愿斯文对汝有所裨益,纵其简陋未及渊博,亦足以略尽绵薄之力。倘若尚存阙漏,敬请不吝斧正,俾便精进!
  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

DTcode7

客官,赏个铜板吧

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

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

打赏作者

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

抵扣说明:

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

余额充值