transition-event.uvue 2.4 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 31 32 33 34 35 36 37 38 39 40
    onReady() {
      // #ifdef APP-ANDROID
      var that = this
      class ThreadRunnable extends Runnable {
        override run() {
          that.isShow = true
        }
      }
      new Thread(new ThreadRunnable()).start()
      // #endif
    },
张磊 已提交
41 42 43 44 45 46 47 48
    methods: {
      switchBtn() {
        if (!this.isStart) {
          if (this.element == null) {
            this.element = uni.getElementById('transition-transform')
          }
          this.buttonValue = "关闭图片旋转"
          this.times = this.times + 1
雪洛's avatar
雪洛 已提交
49
          this.element!.style.setProperty('transition-duration', '2000ms')
50
          this.element!.style.setProperty('transform', 'rotate(' + this.times * 360 + 'deg)')
张磊 已提交
51 52 53 54
          this.isStart = true
        } else {
          this.isStart = false
          this.times = 0
张磊 已提交
55
          this.onTransitionEndTriggr = false
张磊 已提交
56
          this.buttonValue = "开启图片旋转"
雪洛's avatar
雪洛 已提交
57
          this.element!.style.setProperty('transition-duration', '0ms')
58
          this.element!.style.setProperty('transform', 'rotate(' + this.times * 360 + 'deg)')
张磊 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72
        }
      },
      onEnd() {
        if (this.isStart) {
          this.times = this.times + 1
          this.element!.style.setProperty('transform', 'rotate(' + this.times * 360 + 'deg)')
          this.onTransitionEndTriggr = true
        }
      }
    }
  }
</script>

<style>
73 74 75 76 77 78 79 80 81 82 83 84 85 86
  .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);
  }
张磊 已提交
87
</style>