transition-event.uvue 2.6 KB
Newer Older
张磊 已提交
1 2
<template>
  <!-- #ifdef APP -->
3
  <scroll-view style="flex:1;" v-if="isShow">
张磊 已提交
4 5
  <!-- #endif -->
    <image class="transition-transform" id="transition-transform" @transitionend="onEnd" src="/static/uni.png"></image>
6 7
    <text class="adjust">对图片设置transform进行旋转,在旋转完成的transitionend事件后,继续旋转</text>
    <button class="adjust" @click="switchBtn">{{buttonValue}}</button>
张磊 已提交
8 9 10 11 12 13 14 15 16 17
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script>
  export default {
    data() {
      return {
        times: 0,
DCloud-yyl's avatar
DCloud-yyl 已提交
18
        element: null as UniElement | null,
张磊 已提交
19 20
        isStart: false,
        buttonValue: "开启图片旋转",
21
        onTransitionEndTriggr: false,
22 23 24 25 26 27
        // #ifdef APP-ANDROID
        isShow: false,
        // #endif
        // #ifndef APP-ANDROID
        isShow: true,
        // #endif
张磊 已提交
28 29
      }
    },
30
    onReady() {
张磊 已提交
31
      // onReady中动态修改isShow是为了验证在安卓手机上子线程中创建节点可能会崩溃的问题,不具备代码参考性。
32 33 34 35 36 37 38 39 40 41
      // #ifdef APP-ANDROID
      var that = this
      class ThreadRunnable extends Runnable {
        override run() {
          that.isShow = true
        }
      }
      new Thread(new ThreadRunnable()).start()
      // #endif
    },
张磊 已提交
42 43 44 45 46 47 48 49
    methods: {
      switchBtn() {
        if (!this.isStart) {
          if (this.element == null) {
            this.element = uni.getElementById('transition-transform')
          }
          this.buttonValue = "关闭图片旋转"
          this.times = this.times + 1
雪洛's avatar
雪洛 已提交
50
          this.element!.style.setProperty('transition-duration', '2000ms')
51
          this.element!.style.setProperty('transform', 'rotate(' + this.times * 360 + 'deg)')
张磊 已提交
52 53 54 55
          this.isStart = true
        } else {
          this.isStart = false
          this.times = 0
张磊 已提交
56
          this.onTransitionEndTriggr = false
张磊 已提交
57
          this.buttonValue = "开启图片旋转"
雪洛's avatar
雪洛 已提交
58
          this.element!.style.setProperty('transition-duration', '0ms')
59
          this.element!.style.setProperty('transform', 'rotate(' + this.times * 360 + 'deg)')
张磊 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73
        }
      },
      onEnd() {
        if (this.isStart) {
          this.times = this.times + 1
          this.element!.style.setProperty('transform', 'rotate(' + this.times * 360 + 'deg)')
          this.onTransitionEndTriggr = true
        }
      }
    }
  }
</script>

<style>
74 75 76 77 78 79 80 81 82 83 84 85 86 87
  .adjust {
    margin: 10px;
  }

  .transition-transform {
    width: 200px;
    height: 200px;
    margin: 25px auto;
    border-radius: 100px;
    transition-duration: 2000ms;
    transition-property: transform;
    transition-timing-function: linear;
    transform: rotate(0deg);
  }
张磊 已提交
88
</style>