自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

從微信小程序到鴻蒙JS開發(fā)-CSS3動(dòng)畫&JS動(dòng)畫&定時(shí)器

開發(fā)
文章由鴻蒙社區(qū)產(chǎn)出,想要了解更多內(nèi)容請(qǐng)前往:51CTO和華為官方戰(zhàn)略合作共建的鴻蒙技術(shù)社區(qū)https://harmonyos.51cto.com

[[383195]]

想了解更多內(nèi)容,請(qǐng)?jiān)L問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

在進(jìn)入APP時(shí),通常都會(huì)有一個(gè)歡迎界面,用于展示APP的名稱、logo,并預(yù)先加載部分?jǐn)?shù)據(jù)。既然是歡迎頁(yè)面,自然少不了一些動(dòng)畫元素。簡(jiǎn)單運(yùn)用了CSS3和JS的動(dòng)畫效果,progress組件以及倒計(jì)時(shí)擼了一個(gè)歡迎頁(yè)面。直接上效果:

1、基于CSS3的動(dòng)畫效果

1.1 給動(dòng)畫元素設(shè)置animation屬性。

  • animation-name:動(dòng)畫名
  • animation-duration:動(dòng)畫持續(xù)時(shí)間
  • animation-delay:動(dòng)畫開始前延遲時(shí)間
  • animation-iteration-count:動(dòng)畫重復(fù)次數(shù)
  • animation-timing-function:動(dòng)畫執(zhí)行速度
  • animation-fill-mode:動(dòng)畫模式
  1. <image src="/common/huaWei.jpeg" class="logo"></image> 

  1. .logo { 
  2.     width: 300px; 
  3.     height: 300px; 
  4.     border-radius: 150px; 
  5.     animation-name: an1; 
  6.     animation-duration: 5s; 
  7.     animation-iteration-count: 1; 
  8.     animation-timing-function: linear; 
  9.     animation-fill-mode: none; 

1.2 用"@keyframes 動(dòng)畫名"匹配設(shè)置動(dòng)畫規(guī)則。

  1. @keyframes an1 { 
  2.         from { 
  3.             transform: rotate(180deg); 
  4.             opacity: 0.3; 
  5.         } 
  6.         to { 
  7.             transform: rotate(360deg); 
  8.             opacity: 1.0; 
  9.         } 

除from,to外,還可以使用百分比(如20%{...})方式設(shè)置動(dòng)畫途中的效果。

以上兩步,就實(shí)現(xiàn)了gif圖中HUAWEI的logo旋轉(zhuǎn)和逐漸清晰的動(dòng)畫效果。

2、基于JS的動(dòng)畫效果

2.1 動(dòng)畫元素給定id/ref等可以用于元素匹配的屬性。

  1. <image src="/common/liteMall.png" class="textImg" id="textImg"></image> 

2.2 在onShow()方法中獲取元素實(shí)例,并用animate()方法給定動(dòng)畫規(guī)則和基本屬性。注意這一步在onInit()和onReady()中執(zhí)行是沒有效果的。

animate()接受兩個(gè)參數(shù),第一個(gè)為數(shù)組,指定動(dòng)畫的關(guān)鍵幀效果。第二個(gè)為對(duì)象,指定動(dòng)畫的基本屬性。

2.3 調(diào)用play()方法開始動(dòng)畫執(zhí)行。

  1. onShow() { 
  2.      // 設(shè)置動(dòng)畫 
  3.      let textImg = this.$element("textImg").animate([ 
  4.          { 
  5.              transform: {translateY: '200px'}, opacity: 0.1 
  6.          }, 
  7.          { 
  8.              transform: {translateY: '0px'}, opacity: 1 
  9.          } 
  10.      ], { 
  11.          duration: 5000, 
  12.          easing: "linear-out-slow-in"
  13.          fill: "forwards"
  14.          iterations: 1 
  15.      }); 
  16.      textImg.play(); 
  17.      ...... 
  18.  } 

這個(gè)方法在開發(fā)者文檔中未找到說(shuō)明,但證實(shí)可用,且IDE也是有提示的。

transform其中的key輸入?yún)s是沒有提示了。

這里寫完后會(huì)有紅線說(shuō)缺少屬性,但運(yùn)行是沒問題的,可以忽略。如果看著難受可以把數(shù)組單獨(dú)聲明為一個(gè)變量,再作為animate()方法入?yún)ⅰ?/p>

以上三步,就實(shí)現(xiàn)了gif圖中"litemall"字樣從下方上移并逐漸清晰的動(dòng)畫效果。

對(duì)比CSS3的動(dòng)畫技術(shù),使用JS實(shí)現(xiàn)動(dòng)畫會(huì)更有靈活性??梢栽趏nShow()中定義動(dòng)畫,在用戶進(jìn)行一定操作后再執(zhí)行。CSS3的只能在頁(yè)面顯示后一定時(shí)間執(zhí)行,但可以用百分比的形式定義更豐富的動(dòng)畫漸變效果。

3、JS定時(shí)器

setTimeout()和setInterval()兩個(gè)定時(shí)函數(shù)在鴻蒙中可以無(wú)縫對(duì)接使用。

gif圖中的倒計(jì)時(shí)使用setInterval()實(shí)現(xiàn)每1秒倒數(shù)一個(gè)數(shù)并改變省略號(hào)的個(gè)數(shù),在倒數(shù)到0時(shí)清除定時(shí)器。為防止僵尸線程影響性能,切記調(diào)用clearTimeout()和clearInterval()清除掉定時(shí)器。

倒計(jì)時(shí)部分,hml視圖層:

  1. <div class="loading"
  2.     <progress type="circular"></progress> 
  3.     <text> 
  4.         {{ loading }} 
  5.     </text> 
  6. </div> 
  7. <text class="count"
  8.     {{ seconds }} 
  9. </text> 

css渲染層:

  1. .loading { 
  2.     width: 100%; 
  3.     height: 150px; 
  4.     display: flex; 
  5.     justify-content: center; 
  6.     align-items: center; 
  7. progress { 
  8.     width: 120px; 
  9.     height: 120px; 
  10. .loading>text { 
  11.     font-size: 40px; 
  12.     color: #666666; 
  13. .count { 
  14.     position: fixed; 
  15.     bottom: 385px; 
  16.     left: 225px; 
  17.     font-size: 60px; 
  18.     color: #666666; 

js邏輯層:

  1. onShow() { 
  2.   ...... 
  3.       // 設(shè)置倒計(jì)時(shí) 
  4.       let iv = setInterval(() => { 
  5.           let suffix; 
  6.           switch (this.seconds % 3) { 
  7.               case 2: 
  8.               suffix = "..."
  9.               break; 
  10.               case 1: 
  11.               suffix = ".."
  12.               break; 
  13.               default
  14.               suffix = "."
  15.               break; 
  16.           } 
  17.           this.loading = "數(shù)據(jù)加載中" + suffix; 
  18.           this.seconds--; 
  19.           if (this.seconds == 0) { 
  20.               clearInterval(iv); 
  21.           } 
  22.       }, 1000); 
  23.   } 

頁(yè)面會(huì)在動(dòng)畫播放完成后跳轉(zhuǎn)到商城首頁(yè),使用setTimeout()設(shè)置定時(shí)跳轉(zhuǎn)即可。這里在播放動(dòng)畫時(shí)預(yù)加載了首頁(yè)需要的數(shù)據(jù),作為頁(yè)面參數(shù)跳轉(zhuǎn),可以加快商城頁(yè)的展示速度,提升用戶體驗(yàn)。

  1. onInit() { 
  2.      // 首頁(yè)數(shù)據(jù)預(yù)加載 
  3.      // 獲取廣告圖片 
  4.      fetch.fetch({ 
  5.          ...... 
  6.      }); 
  7.      // 獲取推薦商品 
  8.      fetch.fetch({ 
  9.          ...... 
  10.      }); 
  11.      // 獲取一級(jí)分類 
  12.      fetch.fetch({ 
  13.          ...... 
  14.      }); 
  15.  }, 
  16.  onShow() { 
  17.      // 設(shè)置定時(shí)跳轉(zhuǎn) 
  18.      let to = setTimeout(() => { 
  19.          router.replace({ 
  20.              uri: "pages/index/index"
  21.              params: { 
  22.                  ad: this.ad, 
  23.                  newGoods: this.newGoods, 
  24.                  hotGoods: this.hotGoods, 
  25.                  types: this.types 
  26.              } 
  27.          }); 
  28.          clearTimeout(to); 
  29.      }, 6000); 
  30.  ...... 
  31.  } 

4、微信小程序的動(dòng)畫效果

最后寫一寫微信小程序的動(dòng)畫實(shí)現(xiàn),在wxss中同樣支持CSS3的動(dòng)畫屬性:

  1. .happy { 
  2.   font-size: 50rpx; 
  3.   color: #e20a0b; 
  4.   animation-name: an1; 
  5.   animation-duration: 5s; 
  6.   animation-delay: 500ms; 
  7.   animation-iteration-count: infinite; 
  8.   animation-direction: normal; 
  9.   animation-fill-mode: forwards; 
  10.   animation-timing-function: linear; 
  11. @keyframes an1 { 
  12.   from { 
  13.     transform: translateX(0px); 
  14.     opacity: 0.5; 
  15.   } 
  16.   to { 
  17.     transform: translateX(300px); 
  18.     opacity: 1; 
  19.   } 

微信小程序的動(dòng)畫JS實(shí)現(xiàn)方式和鴻蒙有很大不同,是通過(guò)微信提供的API定義并實(shí)現(xiàn)動(dòng)畫。接口提供了豐富的方法,可在開發(fā)者文檔查閱。

  1. Page({ 
  2.  
  3.   /** 
  4.    * 頁(yè)面的初始數(shù)據(jù) 
  5.    */ 
  6.   data: { 
  7.     an2: null 
  8.   }, 
  9.  
  10.   onShow: function () { 
  11.     let an2 = wx.createAnimation({ 
  12.       delay: 500, 
  13.       duration: 5000, 
  14.       timingFunction: 'ease-in-out' 
  15.     }); 
  16.     an2.translate(100, 300).step(); 
  17.     an2.rotate(90).opacity(0.1).step(); 
  18.     this.setData({ 
  19.       an2: an2.export() 
  20.     }) 
  21.   }, 

動(dòng)畫基本屬性作為createAnimation()方法的入?yún)?,?dòng)畫關(guān)鍵幀由一連串的方法流式操作給出,以step()結(jié)束。這里一個(gè)動(dòng)畫的執(zhí)行的時(shí)間是duration給定的時(shí)間。動(dòng)畫對(duì)象需使用export()導(dǎo)出到data中,并和頁(yè)面元素的animation屬性綁定。

  1. <view class="happy" animation="{{ an2 }}"
  2.   新年快樂 
  3. </view

想了解更多內(nèi)容,請(qǐng)?jiān)L問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

 

責(zé)任編輯:jianghua 來(lái)源: 鴻蒙社區(qū)
相關(guān)推薦

2021-03-02 09:29:29

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-23 12:25:26

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-20 09:52:02

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-22 14:56:55

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-25 10:01:19

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-23 12:23:57

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-21 11:09:18

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-23 09:52:42

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-04 13:49:41

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-25 15:13:08

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-05 09:46:16

鴻蒙HarmonyOSjs開發(fā)

2012-09-13 09:24:31

CSSJSjQ

2021-02-07 09:17:24

鴻蒙HarmonyOS應(yīng)用開發(fā)

2017-05-11 15:20:52

CSS3動(dòng)畫前端

2014-04-29 10:39:27

CSS3JavaScript

2012-05-25 10:31:44

HTML5

2011-06-29 13:22:58

CSS3

2015-10-14 09:50:11

css3動(dòng)畫模擬

2022-10-20 11:49:49

JS動(dòng)畫幀,CSS

2013-01-30 16:15:40

adobeHTML5css3
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)