icon.uvue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <template>
  2. <uni-icons :id="elId" class='load-ani' :style="aniStyle" @transitionend="onEnd" :type="iconType" :size="size" :color="color"></uni-icons>
  3. </template>
  4. <script>
  5. export default {
  6. name:'icon',
  7. props: {
  8. iconType: {
  9. type: String,
  10. default: 'loop'
  11. },
  12. size: {
  13. type: Number,
  14. default: 0
  15. },
  16. color: {
  17. type: String,
  18. default: '#333'
  19. }
  20. },
  21. data() {
  22. const elId = `Uni_${(Math.random() * 10e5).toInt().toString(36)}`
  23. return {
  24. elId: elId,
  25. element: null as UniElement | null,
  26. times: 0,
  27. aniStyle: '',
  28. deg: 3600000
  29. }
  30. },
  31. created() {
  32. this.times = 0
  33. // 需要延迟一些时间,否则动画不生效
  34. setTimeout(() => {
  35. this.aniStyle = 'transform:rotate(1deg);'
  36. }, 300)
  37. },
  38. mounted() {
  39. this.element = uni.getElementById(this.elId as string)
  40. },
  41. methods: {
  42. onEnd() {
  43. // 因为循环角度是不断增加,在增加10次以后需要重置,防止无限增加下去
  44. if (this.times == 10) {
  45. this.element!.style.setProperty('transform', 'rotate(0deg)')
  46. this.element!.style.setProperty('transition-duration', '1')
  47. this.times = 0
  48. return
  49. }
  50. this.times = this.times + 1
  51. const rotate = this.times * 360
  52. this.element!.style.setProperty('transform', 'rotate(' + rotate + 'deg)')
  53. this.element!.style.setProperty('transition-duration', '1000')
  54. }
  55. }
  56. }
  57. </script>
  58. <style>
  59. .load-ani {
  60. transition-property: transform;
  61. transition-duration: 0.1s;
  62. transition-timing-function: linear;
  63. transform: rotate(0deg);
  64. }
  65. </style>