国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

mpvue小程序循環動畫開啟暫停的實現方法

2019-11-19 11:34:06
字體:
來源:轉載
供稿:網友

用小程序的 animation 屬性實現循環動畫的開啟與暫停,并封裝到組件。

實現一個字體圖標組件的循環旋轉動畫開啟/暫停

  1. 用于點擊圖標,字體顏色變換,開始循環旋轉動畫,并刷新內容
  2. 刷新結束,停止動畫,并設置字體顏色為原來的
  3. 主要利用 setInterval 定時器循環執行動畫

首先,組件寫出來

添加點擊事件,動畫屬性, style 屬性(用來動態修改樣式)

src/components/refresh.vue

<template> <div>  <div   class="iconfont icon-shuaxin"   :animation='refreshA'   @click="refresh"   :style='style'></div> </div></template>

設置初始數據

使用一個 布爾 數據 refreshing 判斷動畫的狀態為開啟 true /暫停 false

<script>export default { data () {  return {   refreshA: null,   style: 'color: #eee;',   // 用來設置存儲旋轉的度數   rotate: 0,   // 存儲定時器id   refreshI: null  } }, props: ['refreshing']}</script>

添加點擊事件函數

<script>export default { methods: {  // 刷新按鈕點擊  refresh () {   // 正在刷新 則跳出,避免在循環動畫執行時,再次出發點擊刷新事件   if (this.refreshing) return   // 否則提交刷新事件   this.$emit('refresh')  },  // 刷新動畫結束  refreshend () {   // 當動畫結束,字體顏色恢復原來   this.style = 'color: #eee;'  } }}</script>

監聽 refreshing 狀態

<script>export default { watch: {  refreshing (newV, oldV) {   // 沒有正在刷新 > 正在刷新 設置循環動畫   if (newV && !oldV) {    this.style = 'color: #fff;'    this.refreshI = setInterval(() => {    // 每次 +360 實現每 300 毫秒旋轉 360 度      this.rotate += 360     let animation = wx.createAnimation()     animation.rotateZ(this.rotate).step()     this.refreshA = animation.export()    }, 300)    return   }   // 從正在刷新 > 刷新完成 清空循環定時器動畫   if (!newV && oldV) {    clearInterval(this.refreshI)    this.refreshA = null   }  } }}</script>

需要注意的是定時器時間必須和動畫的過渡時間設置為相同

組件調用

src/pages/index/index.vue

<template> <div>  <Refresh @refresh='refresh' :refreshing='refreshing'/> </div></template><script>import Refresh from '@/components/refresh'export default { data: {  // 初始狀態肯定為 false ,點擊刷新組件后,在事件函數中再設置為 true  refreshing: false }, components: {  Refresh }, methods: {  async refresh () {  this.refreshing = true  // 這里為一個異步請求api  let data = await api.getData()  // 請求完成,執行想要操作的代碼后,設置動畫為 false  // this.data = data  this.refreshing = false  } }}</script><style lang="stylus" scoped></style>

refresh 組件完整代碼

<template> <div>  <div   class="iconfont icon-shuaxin"   :animation='refreshA'   @click="refresh"   :style='style'></div> </div></template><script>export default { data () {  return {   refreshA: null,   style: 'color: #eee;',   rotate: 0,   refreshI: null  } }, props: ['refreshing'], watch: {  refreshing (newV, oldV) {   if (newV && !oldV) {    this.style = 'color: #fff;'    this.refreshI = setInterval(() => {     this.rotate += 360     let animation = wx.createAnimation()     animation.rotateZ(this.rotate).step()     this.refreshA = animation.export()    }, 300)    return   }   if (!newV && oldV) {    clearInterval(this.refreshI)    this.refreshA = null   }  } }, methods: {  refresh () {   if (this.refreshing) return   this.$emit('refresh')  },  refreshend () {   this.style = 'color: #eee;'  } }}</script><style lang="stylus" scoped></style>

效果

正常效果,看圖中右上角

網速太快

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 十堰市| 霍城县| 高碑店市| 喜德县| 五台县| 麻栗坡县| 垫江县| 万年县| 宁国市| 沧州市| 嵊州市| 沙河市| 鸡泽县| 巴楚县| 太仓市| 海淀区| 桂林市| 鹿泉市| 壤塘县| 汉川市| 左贡县| 通江县| 娄烦县| 孟村| 托里县| 文山县| 巴马| 龙岩市| 嫩江县| 洛南县| 长岛县| 庄浪县| 瑞昌市| 横峰县| 平舆县| 安图县| 彰武县| 静海县| 丽江市| 安徽省| 德江县|