circle.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <div :style="svgStyle">
  3. <svg :style="svgStyle">
  4. <defs>
  5. <linearGradient id="orange_red" x1="1%" y1="0%" x2="100%" y2="0%">
  6. <stop offset="0%" style="stop-color:#24C3F1;stop-opacity:1" />
  7. <stop offset="100%" style="stop-color:#5EEDCC;stop-opacity:1" />
  8. </linearGradient>
  9. </defs>
  10. <!--内边框 -->
  11. <path
  12. :d="pathString"
  13. :stroke="trailColor"
  14. :stroke-width="trailWidth"
  15. :fill-opacity="0"
  16. />
  17. <!--外边框 -->
  18. <path
  19. :d="pathString"
  20. stroke-linecap="round"
  21. stroke="url(#orange_red)"
  22. :stroke-width="strokeWidth"
  23. fill-opacity="0"
  24. :style="pathStyle"
  25. />
  26. </svg>
  27. <div class="vux-circle-content">
  28. <slot></slot>
  29. </div>
  30. </div>
  31. </template>
  32. <script>
  33. export default {
  34. name: "processCircle",
  35. props: {
  36. strokeWidth: {
  37. // 线条宽度
  38. type: Number,
  39. default: 1
  40. },
  41. strokeColor: {
  42. // 线条颜色
  43. type: String,
  44. default: "#3FC7FA"
  45. },
  46. trailWidth: {
  47. // 背景线条宽度
  48. type: Number,
  49. default: 1
  50. },
  51. trailColor: {
  52. // 背景线条宽度
  53. type: String,
  54. default: "#D9D9D9"
  55. },
  56. percent: {
  57. // 进度百分比
  58. type: Number,
  59. default: 0
  60. },
  61. rd: {
  62. // 圆大小半径
  63. type: Number,
  64. default: 60
  65. }
  66. },
  67. computed: {
  68. svgStyle() {
  69. return {
  70. width: `${2 * this.rd}px`,
  71. height: `${2 * this.rd}px`,
  72. position: "relative"
  73. };
  74. },
  75. radius() {
  76. // 外圆圆半径
  77. return this.rd - this.strokeWidth / 2;
  78. },
  79. pathString() {
  80. return `M ${this.rd},${this.rd} m 0,-${this.radius}
  81. a ${this.radius},${this.radius} 0 1 1 0,${2 * this.radius}
  82. a ${this.radius},${this.radius} 0 1 1 0,-${2 * this.radius}`;
  83. },
  84. len() {
  85. // Math.PI 圆周率 3.141592653589793 len是圆的周长
  86. return Math.PI * 2 * this.radius;
  87. },
  88. pathStyle() {
  89. return {
  90. "stroke-dasharray": `${this.len}px ${this.len}px`,
  91. // eslint-disable-next-line prettier/prettier
  92. "stroke-dashoffset": `${(100 - this.percent) / 100 * this.len}px`, // 动画占圆周长百分比
  93. transition: "stroke-dashoffset 1s ease" // 2s代表动画时间
  94. };
  95. }
  96. }
  97. };
  98. </script>
  99. <style lang="stylus" scoped>
  100. .vux-circle-content {
  101. text-align: center;
  102. position: absolute;
  103. left: 50%;
  104. top: 50%;
  105. transform: translate(-50%, -50%);
  106. span {
  107. font-size: 20px;
  108. font-family: DINAlternate-Bold, DINAlternate;
  109. font-weight: bold;
  110. color: rgba(46, 55, 68, 1);
  111. }
  112. }
  113. </style>