transition-event.uvue 2.0 KB
Newer Older
张磊 已提交
1 2
<template>
  <!-- #ifdef APP -->
3
  <scroll-view style="flex:1;">
张磊 已提交
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 21 22 23 24 25 26 27 28 29 30 31 32
        isStart: false,
        buttonValue: "开启图片旋转",
        onTransitionEndTriggr: false
      }
    },
    methods: {
      switchBtn() {
        if (!this.isStart) {
          if (this.element == null) {
            this.element = uni.getElementById('transition-transform')
          }
          this.buttonValue = "关闭图片旋转"
          this.times = this.times + 1
          this.element!.style.setProperty('transform', 'rotate(' + this.times * 360 + 'deg)')
雪洛's avatar
雪洛 已提交
33
          this.element!.style.setProperty('transition-duration', '2000ms')
张磊 已提交
34 35 36 37 38 39
          this.isStart = true
        } else {
          this.isStart = false
          this.times = 0
          this.buttonValue = "开启图片旋转"
          this.element!.style.setProperty('transform', 'rotate(' + this.times * 360 + 'deg)')
雪洛's avatar
雪洛 已提交
40
          this.element!.style.setProperty('transition-duration', '0ms')
张磊 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54
        }
      },
      onEnd() {
        if (this.isStart) {
          this.times = this.times + 1
          this.element!.style.setProperty('transform', 'rotate(' + this.times * 360 + 'deg)')
          this.onTransitionEndTriggr = true
        }
      }
    }
  }
</script>

<style>
55 56 57 58 59 60 61 62 63 64 65 66 67
.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);
}
张磊 已提交
68
</style>