progress.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <template>
  2. <div class="progress" :style="{
  3. 'width': percent+'%',
  4. 'height': height,
  5. 'background-color': canSuccess? successColor() : failedColor(),
  6. 'opacity': show ? 1 : 0
  7. }"></div>
  8. </template>
  9. <script>
  10. /* eslint-disable */
  11. export default {
  12. data () {
  13. return {
  14. percent: 0,
  15. show: false,
  16. canSuccess: true,
  17. duration: 3000,
  18. height: '2px'
  19. }
  20. },
  21. methods: {
  22. successColor() {
  23. return this.userSelectColor()['$--color-primary'] || '#409EFF';
  24. },
  25. failedColor() {
  26. return this.userSelectColor()['$--color-danger'] || '#F56C6C';
  27. },
  28. userSelectColor() {
  29. return window.userThemeConfig && window.userThemeConfig.global || {}
  30. },
  31. start () {
  32. this.show = true
  33. this.canSuccess = true
  34. if (this._timer) {
  35. clearInterval(this._timer)
  36. this.percent = 0
  37. }
  38. // Code from Nuxt.js!
  39. this._cut = 10000 / Math.floor(this.duration)
  40. this._timer = setInterval(() => {
  41. this.increase(this._cut * Math.random())
  42. if (this.percent >= 90) {
  43. this.percent = 90;
  44. }
  45. }, 100)
  46. return this
  47. },
  48. set (num) {
  49. this.show = true
  50. this.canSuccess = true
  51. this.percent = Math.floor(num)
  52. return this
  53. },
  54. get () {
  55. return Math.floor(this.percent)
  56. },
  57. increase (num) {
  58. this.percent = this.percent + Math.floor(num)
  59. return this
  60. },
  61. decrease (num) {
  62. this.percent = this.percent - Math.floor(num)
  63. return this
  64. },
  65. finish () {
  66. this.percent = 100
  67. this.hide()
  68. return this
  69. },
  70. pause () {
  71. clearInterval(this._timer)
  72. return this
  73. },
  74. hide () {
  75. clearInterval(this._timer)
  76. this._timer = null
  77. setTimeout(() => {
  78. this.show = false
  79. this.$nextTick(() => {
  80. setTimeout(() => {
  81. this.percent = 0
  82. }, 200)
  83. })
  84. }, 500)
  85. return this
  86. },
  87. fail () {
  88. this.canSuccess = false
  89. return this
  90. }
  91. }
  92. }
  93. </script>
  94. <style scoped>
  95. .progress {
  96. position: fixed;
  97. top: 0px;
  98. left: 0px;
  99. right: 0px;
  100. height: 2px;
  101. width: 0%;
  102. transition: width 0.2s, opacity 0.4s;
  103. opacity: 1;
  104. z-index: 999999;
  105. }
  106. </style>