loading.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <template>
  2. <div>
  3. <div class="loading-overlay" v-if="isLoading">
  4. <div class="loading-content">
  5. <el-progress type="circle" :percentage="percentage"></el-progress>
  6. </div>
  7. </div>
  8. <div>
  9. <slot>
  10. </slot>
  11. </div>
  12. </div>
  13. </template>
  14. <script>
  15. export default {
  16. name: 'CustomLoading',
  17. props: {
  18. },
  19. data() {
  20. return {
  21. isLoading: false,
  22. percentage: 0,
  23. timer: null,
  24. timer2: null,
  25. timer3: null,
  26. };
  27. },
  28. created() {
  29. },
  30. methods: {
  31. getLoading() {
  32. this.timer = setInterval(() => {
  33. if (this.percentage < 60) {
  34. this.percentage += 1;
  35. } else {
  36. clearTimeout(this.timer);
  37. this.slowLoading()
  38. }
  39. }, 100);
  40. },
  41. slowLoading() {
  42. this.timer2 = setInterval(() => {
  43. if (this.percentage < 90) {
  44. this.percentage += 1;
  45. } else {
  46. clearTimeout(this.timer2);
  47. this.SlowestLoading()
  48. }
  49. }, 300);
  50. },
  51. SlowestLoading() {
  52. this.timer3 = setInterval(() => {
  53. if (this.percentage < 98) {
  54. this.percentage += 1;
  55. } else {
  56. clearTimeout(this.timer3);
  57. }
  58. }, 1000);
  59. },
  60. startLoading() {
  61. this.isLoading = true
  62. this.getLoading()
  63. },
  64. endLoading() {
  65. if (this.percentage < 60) {
  66. clearTimeout(this.timer);
  67. this.percentage = 100
  68. } else if (this.percentage < 98) {
  69. clearTimeout(this.timer2);
  70. this.percentage = 100
  71. } else {
  72. clearTimeout(this.timer3);
  73. this.percentage = 100
  74. }
  75. setTimeout(() => {
  76. this.percentage = 0
  77. this.isLoading = false
  78. }, 500);
  79. }
  80. }
  81. }
  82. </script>
  83. <style scoped>
  84. .loading-overlay {
  85. position: absolute;
  86. z-index: 2000;
  87. background-color: hsla(0, 0%, 100%, .9);
  88. margin: 0;
  89. top: 0;
  90. right: 0;
  91. bottom: 0;
  92. left: 0;
  93. transition: opacity .3s;
  94. }
  95. .loading-content {
  96. display: flex;
  97. justify-content: center;
  98. align-items: center;
  99. height: 100%;
  100. }
  101. </style>