微信小程序可實(shí)時(shí)改變轉(zhuǎn)速的css3旋轉(zhuǎn)動(dòng)畫

先上效果圖
LcFireRabbit

最上面那一行就是個(gè)簡(jiǎn)單的換顏色效果,極其簡(jiǎn)答就不多說了,直接上代碼。

WXML

<view class='box' style='background-color:{{backgroundcolor}}'>
</view>
<view class='viewBox'>
  <button bindtap='changeColor' data-color='black' class='box'>黑</button>
  <button bindtap='changeColor' data-color='violet' class='box'>紫</button>
  <button bindtap='changeColor' data-color='orange' class='box'>橙</button>
  <button bindtap='changeColor' data-color='blue' class='box'>藍(lán)</button>
  <button bindtap='changeColor' data-color='green' class='box'>綠</button>
</view>

JS

  data: {
    backgroundcolor:'red'
  },
  changeColor:function(e){
    this.setData({
      backgroundcolor: e.currentTarget.dataset.color
    })
  }
那么下面咱們說一說這個(gè)旋轉(zhuǎn)的動(dòng)畫。小程序里呢,有自己的動(dòng)畫api,但是用起來感覺極其麻煩,而且容易產(chǎn)生倒轉(zhuǎn),對(duì)設(shè)備的性能消耗也多,動(dòng)畫多了以后就會(huì)極其卡頓,所以還是css3的動(dòng)畫比較好。

首先來寫這個(gè)css3動(dòng)畫

css3旋轉(zhuǎn)動(dòng)畫

<view class='animationSlow'></view>
.animationSlow {
   width: 100rpx;
  height: 100rpx;
  background-color: orange;
  animation-name: myfirst; /*動(dòng)畫的名稱 */
  animation-duration: 2000ms; /*動(dòng)畫從開始到結(jié)束的時(shí)間*/
  animation-timing-function: linear; /*動(dòng)畫執(zhí)行快慢的參數(shù)*/
  animation-iteration-count: infinite; /*動(dòng)畫執(zhí)行多少次的參數(shù)*//*以下是兼容ios所需,參數(shù)意義與上相同*/
  -webkit-animation-name: myfirst;
  -webkit-animation-duration: 2000ms;
  -webkit-animation-timing-function: linear;
  -webkit-animation-iteration-count: infinite;
}
@keyframes myfirst {
  /*開始轉(zhuǎn)的角度*/
  from {
    transform: rotate(0deg);
  }/*結(jié)束的角度*/
  to {
    transform: rotate(360deg);
  }
}
/*兼容ios*/
@-webkit-keyframes myfirst {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
效果圖

如果只是一個(gè)一次性的動(dòng)畫效果,現(xiàn)在這些代碼就OK了。
如果想要實(shí)時(shí)可以改變旋轉(zhuǎn)的轉(zhuǎn)速,我們還得再加點(diǎn)東西。

實(shí)現(xiàn)可以實(shí)時(shí)修改轉(zhuǎn)速

微信小程序里涉及到實(shí)時(shí)數(shù)據(jù)就避免不了Page.data這個(gè)東西。
1.我們需要將控制動(dòng)畫時(shí)間的css屬性放到內(nèi)聯(lián)樣式中去

<view class='animationSlow' style='animation-duration: 2000ms;-webkit-animation-duration: 2000ms;'></view>

2.在頁面對(duì)應(yīng)的js中,設(shè)置掌控時(shí)間的Page.data屬性,將wxml里內(nèi)聯(lián)屬性的時(shí)間改為Page.data的屬性。

  data: {
    animationTime:'2000ms'
  },
<view class='animationSlow' style='animation-duration: {{animationTime}};-webkit-animation-duration: {{animationTime}};'></view>

3.接下來我們寫幾個(gè)按鈕,綁定上修改這個(gè)時(shí)間的方法,進(jìn)而來改變轉(zhuǎn)速。這一步都是基本代碼,我就不貼代碼了。放個(gè)效果圖吧。


效果圖

那么接下來重點(diǎn)來了:其實(shí)這里有個(gè)bug,這個(gè)效果呢在安卓機(jī)上是一點(diǎn)點(diǎn)問題都沒有的。但是在蘋果機(jī)上,動(dòng)畫一旦開始,再通過這個(gè)方法去修改轉(zhuǎn)速,就不能生效了。

解決IOS系統(tǒng)的BUG

上面說了,IOS系統(tǒng)上呢,動(dòng)畫一旦開始,這個(gè)方法就不能用了。那么咱是不是可以先把這個(gè)動(dòng)畫停下來,然后再改轉(zhuǎn)速呢?這個(gè)辦法可不可以呢?答案是肯定的,但是不是去把動(dòng)畫時(shí)間改為0,而是采用了css3動(dòng)畫的一個(gè)屬性。CSS3 動(dòng)畫教程

animation-play-state: paused|running;

簡(jiǎn)而言之就是先用這個(gè)屬性把動(dòng)畫暫停,修改轉(zhuǎn)速,然后再讓它跑起來。這一切都得再js里進(jìn)行。
1.需要在標(biāo)簽的內(nèi)聯(lián)樣式里加上這個(gè)屬性,在Page.data里再定義一個(gè)屬性控制開始暫停。

<view class='animationSlow' style='animation-duration: {{animationTime}};-webkit-animation-duration: {{animationTime}};animation-play-state:{{status}};-webkit-animation-play-state:{{status}};'></view>
 data: {
    animationTime:'2000ms',
    status: 'running'//paused
  },

2.然后我們?nèi)バ薷母淖冝D(zhuǎn)速的方法。暫停>(修改>跑起來),效果上稍微有些延遲。

 changeTime:function(e){
    this.setData({
      status: 'paused'
    })
    this.setData({
      timeAnimation: e.currentTarget.dataset.time,
      status: 'running'
    })
  },

3.來上效果圖了。


效果圖

可能動(dòng)圖上感覺不出來,不過你們可以去真機(jī)試一下,就可以展現(xiàn)出來了。

本次分享結(jié)束(@LcFireRabbit)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、通過CocoaPods安裝項(xiàng)目名稱項(xiàng)目信息 AFNetworking網(wǎng)絡(luò)請(qǐng)求組件 FMDB本地?cái)?shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,228評(píng)論 3 119
  • 這是一對(duì)普通的父子背影。拍攝于一個(gè)早上。很普通,沒有朱自清作品的深刻,似乎看不出有什么故事。 但是,今天我要講的就...
    珠落玉盤a閱讀 321評(píng)論 0 0
  • 《誰的青春不迷?!非啻合盗械牡谌緯断蛑饬聊欠健?《誰的青春不迷茫》主題是迷茫,以2004-2013年這十年為...
    阿靜成長記閱讀 3,718評(píng)論 4 2
  • 1目前這個(gè)職位最緊要的任務(wù)是什么?如果我有幸貴公司,您希望我三個(gè)月完成哪些工作? 3. 這個(gè)職位在貴公司的具體職責(zé)...
    美楠記閱讀 723評(píng)論 0 0
  • 讀博選擇的原因一: 自己過得很順,一直沒遇到挫折,想挑戰(zhàn)一下自己。想堅(jiān)持做一件事情。想掌握一個(gè)領(lǐng)域的知識(shí)。鍛煉思維...
    蘭陵嘿嘿生閱讀 260評(píng)論 0 0

友情鏈接更多精彩內(nèi)容