image元件動畫問題

華為開發者論壇發表於2021-08-03

現象描述:

stack 元件下使用兩個image 元件堆疊,一個image 元件透過動畫樣式設定透明度從1-0 ,隱藏起來,另一張顯示出來,從而來實現圖片切換,前一張圖片會機率性的閃現然後消失。

問題程式碼如下:

<template>
  <div class="page-wrapper">
    <input type="button" class="button" onclick="onCallAnimationClick" value="Animation 動畫" />
    <stack style="width:400px;height:400px">
    <image class="img" id="img1" src="{{imgUrl}}" oncomplete="imgcomplete"></image>
     <image class="img" id="img2" if="{{ximg}}" src="{{preUrl}}" oncomplete="imgcomplete2" style="{{'opacity:' + preop + ';'}}"></image>
    </stack>
  </div>
</template>
 
<script>
export default {
  data: {
     imgsrc: ["
    "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.aiimg.com%2Fuploads%2Fuserup%2F0909%2F2Z64022L38.jpg&refer=http%3A%2F%2Fimg.aiimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1611368024&t=879ac47e0bd97e413b5462946d9ae4ed",
    "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic3.16pic.com%2F00%2F01%2F11%2F16pic_111395_b.jpg&refer=http%3A%2F%2Fpic3.16pic.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1611368024&t=70c702a55248113dafa6e243dc253625",
    "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fa2.att.hudong.com%2F27%2F81%2F01200000194677136358818023076.jpg&refer=http%3A%2F%2Fa2.att.hudong.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1611368024&t=11199190c6ac8e3371f16d7568d40daa",
     "
     "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fa3.att.hudong.com%2F92%2F04%2F01000000000000119090475560392.jpg&refer=http%3A%2F%2Fa3.att.hudong.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1611368024&t=3b33c5b006bcba88a24d03cfbca1ad20",
     "
     "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fa1.att.hudong.com%2F62%2F02%2F01300542526392139955025309984.jpg&refer=http%3A%2F%2Fa1.att.hudong.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1611368024&t=6500d50b1680d27bb60fe044e90ce41b",       
            ],
    imgUrl:'',
    preUrl:'',
    ximg:true,
    preop:0,
    i:0
  },
  onInit: function () {
      this.imgUrl = this.imgsrc[0]
  },
  onCallAnimationClick() {
     if(this.i > 6){
     this.i = 0 ;
     this.imgUrl = this.imgsrc[this.i]
   }else{
        this.i++
   this.imgUrl = this.imgsrc[this.i]
   }
 
   console.log('imgcomplete=',this.i)
   },
   imgcomplete(){
    console.log('imgcomplete 1')
      this.preop = 1
      var options = {
        duration: 500,
        easing: 'linear',
        delay: 0,
        fill: 'forwards',
        iterations: 1
      }
 
      var frames = [{
        opacity: 1
      },
      {
        opacity: 0
      }];
      var animation = this.$element('img2').animate(frames, options);
      animation.play();
       var self = this
      animation.onfinish = function () {
        console.log("imgcomplete animation  onfinish");
        self.ximg = false
        self.preop = 0
        setTimeout(() => {
          self.ximg = true
          self.preUrl = self.$element("img1").attr.src
        }, 30);
 
      }
    },
  imgcomplete2() {
      console.log('imgcomplete 2')
      setTimeout(() => {
        this.preop = 1
      }, 50);
    },
}
</script>
 
<style>
.page-wrapper {
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.img{
  width:100%;
  height:100%;
}
.button {
  color: #20a0ff;
  background-color: #ffffff;
  padding: 10px 20px;
  border: 1px solid #20a0ff;
  border-radius: 40px;
}
</style>


問題分析:

上述程式碼用兩個image 元件實現了圖片切換時淡入淡出的動畫效果,主要是透過透明度實現的。第二個image css 中設定了透明度,但是imgcomplete() 方法中又對該image 元件做了透明度動畫,透明度值從1 0 ,同時程式碼中又將css 中繫結的透明度變數preop 設定為1

當動畫方法完成時間先於css ,就會出現這個情況。

解決方法:

template 中第二個image 元件的style="{{'opacity:' + preop + ';'}}" 去掉,改為透過動畫樣式來呼叫,從0-1 變化。

修改程式碼如下:

<template>
  <div class="page-wrapper">
    <input type="button" class="button" onclick="onCallAnimationClick" value="Animation 動畫" />
    <stack style="width:400px;height:400px">
    <image class="img" id="img1" src="{{imgUrl}}" oncomplete="imgcomplete"></image>
     <image class="img" id="img2" if="{{ximg}}" src="{{preUrl}}" oncomplete="imgcomplete2" ></image>
    </stack>
  </div>
</template>
 
<script>
export default {
  data: {
    imgsrc: ["
    "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.aiimg.com%2Fuploads%2Fuserup%2F0909%2F2Z64022L38.jpg&refer=http%3A%2F%2Fimg.aiimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1611368024&t=879ac47e0bd97e413b5462946d9ae4ed",
    "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic3.16pic.com%2F00%2F01%2F11%2F16pic_111395_b.jpg&refer=http%3A%2F%2Fpic3.16pic.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1611368024&t=70c702a55248113dafa6e243dc253625",
    "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fa2.att.hudong.com%2F27%2F81%2F01200000194677136358818023076.jpg&refer=http%3A%2F%2Fa2.att.hudong.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1611368024&t=11199190c6ac8e3371f16d7568d40daa",
     "
     "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fa3.att.hudong.com%2F92%2F04%2F01000000000000119090475560392.jpg&refer=http%3A%2F%2Fa3.att.hudong.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1611368024&t=3b33c5b006bcba88a24d03cfbca1ad20",
     "
     "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fa1.att.hudong.com%2F62%2F02%2F01300542526392139955025309984.jpg&refer=http%3A%2F%2Fa1.att.hudong.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1611368024&t=6500d50b1680d27bb60fe044e90ce41b",       
            ],
    imgUrl:'',
    preUrl:'',
    ximg:true,
    preop:0,
    i:0
  },
  onInit: function () {
      this.imgUrl = this.imgsrc[0]
  },
      ...     //省略
  imgcomplete2() {
      console.log('imgcomplete 2')
      var options = {
        duration: 10,
        easing: 'linear',
        delay: 0,
        fill: 'forwards',
        iterations: 1
      }
      var frames = [{
        opacity: 0
      },
      {
        opacity: 1
      }];
      var animation = this.$element('img2').animate(frames, options);
      animation.play();
    },
}
</script>


欲瞭解更多詳情,請參見:

快應用動畫:

https://developer.huawei.com/consumer/cn/doc/development/quickApp-References/quickapp-api-animate

快應用通用樣式:

https://developer.huawei.com/consumer/cn/doc/development/quickApp-References/quickapp-common-settings#h1-1575449213432

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69970551/viewspace-2784918/,如需轉載,請註明出處,否則將追究法律責任。

相關文章