小程序菜单按钮向左弹出动画的实现

需求:实现一个点击菜单悬浮按钮,向左弹出子菜单的动画

最终效果展示:
开始状态.png

弹出状态.png

需求分析:这里一共有6个动画效果,其中包括:主菜单按钮、4个子菜单按钮(动画效果类似)、和背景动画效果。

主菜单按钮动画:实现.rotateZ从 Z 轴顺时针旋转一个角度动画
4个子菜单按钮动画:实现translate平移动画
背景图动画:实现translate平移动画、并且动态设置背景宽度

分析完了,下面是具体代码:
js代码:

// components/Menu/menu.js
var systemInfo = wx.getSystemInfoSync();
Component({
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
      this.setData({
        systemInfo: systemInfo

      })
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    isShow: false,//是否已经弹出
    animMain: {},//旋转动画
    animImg: {},//原始影像动画
    animMedicalhistory: {},// 病历文书动画
    animInspection:{},//检验报告动画
    animcheck:{},//检查报告动画
    animItem:{},//图标背景动画
    animTime: 300,//动画持续时间,单位 ms
    timingFunction_start: 'ease-out',//动画的效果
    timingFunction_end: 'ease-out'//动画的效果


  },

  /**
   * 组件的方法列表
   */
  methods: {
    //点击弹出或者收起
    showOrHide: function () {
      if (this.data.isShow) {
        //缩回动画
        this.takeback();
        this.setData({
          isShow: false,

        })
      } else {
        //弹出动画
        this.popp();
        this.setData({
          isShow: true,

        })
      }
    },
    img: function () {
      this.triggerEvent("img")
      this.showOrHide()
    },
    medicalhistory: function () {
      this.triggerEvent("medicalhistory")
      this.showOrHide()
    },

    inspection: function () {
      this.triggerEvent("inspection")
      this.showOrHide()
    },
    check: function () {
      this.triggerEvent("check")
      this.showOrHide()
    },
    //弹出动画
    popp: function () {
      //main按钮顺时针旋转
      var animationMain = wx.createAnimation({
        duration: this.data.animTime,
        timingFunction: this.data.timingFunction_start
      })
      var animationMedicalhistory = wx.createAnimation({
        duration: this.data.animTime,
        timingFunction: this.data.timingFunction_start
      })
      var animationImg = wx.createAnimation({
        duration: this.data.animTime,
        timingFunction: this.data.timingFunction_start
      })

      var animationInspection = wx.createAnimation({
        duration: this.data.animTime,
        timingFunction: this.data.timingFunction_start
      })

      var animationCheck = wx.createAnimation({
        duration: this.data.animTime,
        timingFunction: this.data.timingFunction_start
      })

      var animationItem= wx.createAnimation({
        duration: this.data.animTime,
        timingFunction: this.data.timingFunction_start
      })


      animationMain.rotateZ(180).step();
      animationCheck.translate(-systemInfo.windowWidth /2, 0).step();
      animationInspection.translate(-systemInfo.windowWidth / 2.5, 0).step();
      animationMedicalhistory.translate(-systemInfo.windowWidth/3.5, 0).step();
      animationImg.translate(-systemInfo.windowWidth/6, 0).step();

      animationItem.translate(-systemInfo.windowWidth / 7, 0).width(systemInfo.windowWidth / 2).step();

      this.setData({
        animMain: animationMain.export(),
        animMedicalhistory: animationMedicalhistory.export(),
        animImg: animationImg.export(),
        animInspection: animationInspection.export(),
        animCheck: animationCheck.export(),
        animItem: animationItem.export(),



      })
    },
    //收回动画
    takeback: function () {
      //main按钮逆时针旋转
      var animationMain = wx.createAnimation({
        duration: this.data.animTime,
        timingFunction: this.data.timingFunction_end
      })
      var animationMedicalhistory = wx.createAnimation({
        duration: this.data.animTime,
        timingFunction: this.data.timingFunction_end
      })
      var animationImg = wx.createAnimation({
        duration: this.data.animTime,
        timingFunction: this.data.timingFunction_end
      })
      var animationInspection = wx.createAnimation({
        duration: this.data.animTime,
        timingFunction: this.data.timingFunction_end
      })
      var animationCheck = wx.createAnimation({
        duration: this.data.animTime,
        timingFunction: this.data.timingFunction_end
      })
      var animationItem = wx.createAnimation({
        duration: this.data.animTime,
        timingFunction: this.data.timingFunction_end
      })

      animationMain.rotateZ(0).step();
      animationCheck.translate(0, 0).rotateZ(0).step();
      animationInspection.translate(0, 0).rotateZ(0).step();
      animationMedicalhistory.translate(0, 0).rotateZ(0).step();
      animationImg.translate(0, 0).rotateZ(0).step();
      animationItem.translate(0, 0).width(-systemInfo.windowWidth).step();

      this.setData({
        animMain: animationMain.export(),
        animMedicalhistory: animationMedicalhistory.export(),
        animImg: animationImg.export(),
        animInspection: animationInspection.export(),
        animCheck: animationCheck.export(),
        animItem: animationItem.export(),

      })
    }
  },
  //解决滚动穿透问题
  myCatchTouch: function () {
    return
  }
})

wxml代码:

<view class="drawer_screen" bindtap="showOrHide" wx:if="{{isShow}}" catchtouchmove="myCatchTouch"></view>
<view>
  <view class="item-class"  animation="{{animItem}}"></view>

<image src="../../images/icon/imgdetails_icon_check@2x.png" class="buttom" animation="{{animCheck}}" bindtap="check"></image>
<image src="../../images/icon/imgdetails_icon_inspection@2x.png" class="buttom" animation="{{animInspection}}" bindtap="inspection"></image>
    <image src="../../images/icon/imgdetails_icon_medicalhistory@2x.png" class="buttom" animation="{{animMedicalhistory}}" bindtap="medicalhistory"></image>
    <image src="../../images/icon/imgdetails_icon_img@2x.png" class="buttom" animation="{{animImg}}" bindtap="img"></image>
    <image src="../../images/icon/imgdetails_icon_menu@2x.png" class="main-buttom" animation="{{animMain}}" bindtap="showOrHide"></image>
</view>

wxss代码:

/* components/menu/index.wxss */
.main-buttom{
  width: 100rpx;
  height: 100rpx;
  display: flex;
  flex-direction: row;
  position: fixed;
  bottom:220rpx;
  right: 60rpx;
  z-index: 1001;
}
.buttom{
  width: 50rpx;
  height: 50rpx;
  display: flex;
  flex-direction: row;
  position: fixed;
  bottom:244rpx;
  right: 80rpx;
  z-index: 1001;
}
.drawer_screen {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  right:0;
  bottom:0;
  z-index: 1000;
  background: #000;
  opacity: 0;
  overflow: hidden;
}
.drawer_box {
  overflow: hidden;
  position: fixed;
  z-index: 1001;
}


.item-class {
position: fixed;
bottom: 225rpx;
right: 60rpx;
background: white;
border: 1px solid white;
border-radius: 40rpx;
box-shadow: 0px 0px 30px rgba(0, 0, 0, 0.1);
height: 85rpx;

}

page页面调用:

  <menu hidden id='menu' 
       bind:img="onImg" 
       bind:medicalhistory="onMedicalhistory" 
       bind:inspection="onInspection"
       bind:check="onCheck" />
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容

  • 【Android 动画】 动画分类补间动画(Tween动画)帧动画(Frame 动画)属性动画(Property ...
    Rtia阅读 5,954评论 1 38
  • 看了很多视频、文章,最后却通通忘记了,别人的知识依旧是别人的,自己却什么都没获得。此系列文章旨在加深自己的印象,因...
    DCbryant阅读 1,811评论 0 4
  • 最近一个月都在做微信小程序,作为一个Android开发者,在这一个月很少写Android的代码,真是悲催,好在现在...
    Code4Android阅读 7,801评论 4 13
  • 前情回顾 我和乔东是通过前同事唐志军认识的,那时候我刚到现在这家公司,实在无法忍受租住的地下室了,就询问有没有认识...
    狗一样的污姐阅读 238评论 1 2
  • 迭代器 可用于集合,列表,元组,字典,字符串 可以用于节省内存,用完一个数据就可以del掉 可循环的对象统称为:I...
    Luo_Luo阅读 240评论 0 0