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

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