transition-event.uvue 1.9 KB
Newer Older
张磊 已提交
1 2
<template>
  <!-- #ifdef APP -->
W
wanganxp 已提交
3
  <scroll-view style="flex:1;padding: 10px;">
张磊 已提交
4 5 6
  <!-- #endif -->
    <button @click="switchBtn">{{buttonValue}}</button>
    <image class="transition-transform" id="transition-transform" @transitionend="onEnd" src="/static/uni.png"></image>
W
wanganxp 已提交
7
    <text>对图片设置transform进行旋转,在旋转完成的transitionend事件后,继续旋转</text>
张磊 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script>
  export default {
    data() {
      return {
        times: 0,
        element: null as Element | null,
        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)')
          this.element!.style.setProperty('transition-duration', '2000')
          this.isStart = true
        } else {
          this.isStart = false
          this.times = 0
          this.buttonValue = "开启图片旋转"
          this.element!.style.setProperty('transform', 'rotate(' + this.times * 360 + 'deg)')
          this.element!.style.setProperty('transition-duration', '0')
        }
      },
      onEnd() {
        if (this.isStart) {
          this.times = this.times + 1
          this.element!.style.setProperty('transform', 'rotate(' + this.times * 360 + 'deg)')
          this.onTransitionEndTriggr = true
        }
      }
    }
  }
</script>

<style>
  .transition-transform {
    width: 400rpx;
    height: 400rpx;
    margin: 50rpx auto;
    border-radius: 200rpx;
    transition-duration: 2000;
    transition-property: transform;
张磊 已提交
62
    transition-timing-function: linear;
张磊 已提交
63 64 65
    transform: rotate(0deg);
  }
</style>