transition-events.uvue 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view style="flex:1;" v-if="isShow">
  4. <!-- #endif -->
  5. <image class="transition-transform" id="transition-transform" @transitionend="onEnd" src="/static/uni.png"></image>
  6. <text class="adjust">对图片设置transform进行旋转,在旋转完成的transitionend事件后,继续旋转</text>
  7. <button class="adjust" @click="switchBtn">{{buttonValue}}</button>
  8. <!-- #ifdef APP -->
  9. </scroll-view>
  10. <!-- #endif -->
  11. </template>
  12. <script>
  13. export default {
  14. data() {
  15. return {
  16. times: 0,
  17. element: null as UniElement | null,
  18. isStart: false,
  19. buttonValue: "开启图片旋转",
  20. onTransitionEndTriggr: false,
  21. isShow: false
  22. }
  23. },
  24. onReady() {
  25. // onReady中动态修改isShow是为了验证在安卓手机上子线程中创建节点可能会崩溃的问题,不具备代码参考性。
  26. // #ifdef APP-ANDROID
  27. var that = this
  28. class ThreadRunnable extends Runnable {
  29. override run() {
  30. that.isShow = true
  31. }
  32. }
  33. new Thread(new ThreadRunnable()).start()
  34. // #endif
  35. // #ifndef APP-ANDROID
  36. this.isShow = true
  37. // #endif
  38. },
  39. methods: {
  40. switchBtn() {
  41. if (!this.isStart) {
  42. if (this.element == null) {
  43. this.element = uni.getElementById('transition-transform')
  44. }
  45. this.buttonValue = "关闭图片旋转"
  46. this.times = this.times + 1
  47. this.element!.style.setProperty('transition-duration', '2000ms')
  48. this.element!.style.setProperty('transform', 'rotate(' + this.times * 360 + 'deg)')
  49. this.isStart = true
  50. } else {
  51. this.isStart = false
  52. this.times = 0
  53. this.onTransitionEndTriggr = false
  54. this.buttonValue = "开启图片旋转"
  55. this.element!.style.setProperty('transition-duration', '0ms')
  56. this.element!.style.setProperty('transform', 'rotate(0deg)')
  57. }
  58. },
  59. onEnd() {
  60. console.log('transform transitionend')
  61. if (this.isStart) {
  62. this.times = this.times + 1
  63. this.element!.style.setProperty('transform', 'rotate(' + this.times * 360 + 'deg)')
  64. this.onTransitionEndTriggr = true
  65. }
  66. }
  67. }
  68. }
  69. </script>
  70. <style>
  71. .adjust {
  72. margin: 10px;
  73. }
  74. .transition-transform {
  75. width: 200px;
  76. height: 200px;
  77. margin: 25px auto;
  78. border-radius: 100px;
  79. transition-property: transform;
  80. transition-timing-function: linear;
  81. transform: rotate(0deg);
  82. }
  83. </style>