让前端飞

【十】环形进度条组件

2020-02-25  本文已影响0人  大海爱奔跑

关于专题【vue开发音乐App】

本篇介绍底部快捷控件的环形进度条的实现方法:通过svg绘制两个环,线条模糊的环作为背景、线条明亮的环根据传入的百分比绘制相应长度(即进度)。该组件同样属于基础组件,在src/base/progress-circle/目录下新建progress-circle.vue

样式部分

<style lang="stylus" rel="stylesheet/stylus">
  // variable.styl传送门:https://wy310.cn/2020/01/11/vue-build-basic-style-structure/
  @import "~common/stylus/variable"

  .progress-circle
    position: relative
    circle
      stroke-width: 8px
      transform-origin: center
      &.progress-bg
        transform: scale(0.9)
        stroke: $color-theme-d
      &.progress-bar
        transform: scale(0.9) rotate(-90deg)
        stroke: $color-theme
</style>

DOM部分

<template>
  <div class="progress-circle">
    <svg :width="length" :height="length" viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg">
      <!--背景圆环:线条模糊-->
      <circle class="progress-bg" r="50" cx="50" cy="50" fill="transparent"></circle>
      <!--进度圆环:线条明亮-->
      <circle class="progress-bar" r="50" cx="50" cy="50" fill="transparent" :stroke-dasharray="dasharray" :stroke-dashoffset="dashoffset"></circle>
    </svg>
  </div>
</template>

JS部分

<script>
  export default {
    name: 'progress-circle',
    data () {
      return {
        dasharray: Math.PI * 100
      }
    },
    props: {
      // svg边长
      length: {
        type: Number,
        default: 80
      },
      percent: {
        type: Number,
        default: 0
      }
    },
    computed: {
      dashoffset () {
        return (1 - this.percent) * this.dasharray
      }
    }
  }
</script>
上一篇下一篇

猜你喜欢

热点阅读