微信小程序—悬浮球弹出多个item

悬浮球弹出多个item这个效果应用的场景还是挺多的,小程序中实现比较简单,思路也比较清晰,所以写这个demo不为别的,只为熟悉一下小程序的动画操作。

效果:

在这里插入图片描述

体验:

在这里插入图片描述

实现要求:

1、悬浮球固定悬浮在屏幕右下角
2、点击悬浮球成扇形弹出多个items,并扇形排列(弹出效果由透明到不透明、由小到大)
3、item可点击
4、再次点击悬浮球或者点击屏幕空白位置,items沿着原路收回

实现思路:

1、利用position:fixed布局与z-index属性将悬浮球、多个item与透明遮罩重叠放至屏幕右下角,悬浮球处于最顶层
2、点击悬浮球后,各个item执行平移(提前计算好扇形尺寸)、缩放、透明度动画,遮罩执行缩放覆盖全屏动画
3、再次点击悬浮球或者透明遮罩后,各个item与遮罩执行原路返回动画
4、增加item点击事件

代码:

js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow: false,
    animationContact: {},
    animationSave: {},
    animationShare: {},
    animationHome: {},
    animationModal: {},
    animationContainer:{}
  },
  contorlAnimate(){
    if(this.data.isShow){
      this.closeAnimate()
    }else{
      this.showAnimate()
    }
  },
  closeAnimate() {
    var animationModal = wx.createAnimation({
      duration: 200,
      timingFunction: 'ease-in'
    })
    animationModal.opacity(0).scale(0.0, 0.0).step()

    var animationContact = wx.createAnimation({
      duration: 200,
      timingFunction: 'ease-in'
    })
    animationContact.opacity(0).scale(0.0, 0.0).translateX(0).step()

    var animationSave = wx.createAnimation({
      duration: 200,
      timingFunction: 'ease-in'
    })
    animationSave.opacity(0).scale(0.0, 0.0).translateX(0).step()

    var animationShare = wx.createAnimation({
      duration: 200,
      timingFunction: 'ease-in'
    })
    animationShare.opacity(0).scale(0.0, 0.0).translateX(0).step()

    var animationHome = wx.createAnimation({
      duration: 200,
      timingFunction: 'ease-in'
    })
    animationHome.opacity(0).scale(0.0, 0.0).translateX(0).step()
    this.data.isShow = false
    this.setData({
      animationContact: animationContact.export(),
      animationSave: animationSave.export(),
      animationShare: animationShare.export(),
      animationHome: animationHome.export(),
      animationModal: animationModal.export()
    })
  },
  showAnimate() {
    var animationModal = wx.createAnimation({
      duration: 200,
      timingFunction: 'ease-out'
    })
    animationModal.opacity(0.0).scale(300, 300).step()
    var animationContact = wx.createAnimation({
      duration: 200,
      timingFunction: 'ease-out'
    })
    animationContact.opacity(1).scale(0.8, 0.8).translateX(-120).step()
    var animationSave = wx.createAnimation({
      duration: 200,
      timingFunction: 'ease-out'
    })
    animationSave.opacity(1).scale(0.8, 0.8).translateX(-104).translateY(-60).step()

    var animationShare = wx.createAnimation({
      duration: 200,
      timingFunction: 'ease-out'
    })
    animationShare.opacity(1).scale(0.8, 0.8).translateX(-60).translateY(-104).step()

    var animationHome = wx.createAnimation({
      duration: 200,
      timingFunction: 'ease-out'
    })
    animationHome.opacity(1).scale(0.8, 0.8).translateX(0).translateY(-120).step()
    this.data.isShow = true
    this.setData({
      animationContact: animationContact.export(),
      animationSave: animationSave.export(),
      animationShare: animationShare.export(),
      animationHome: animationHome.export(),
      animationModal: animationModal.export()
    })
  },
  clickHome(){
    wx.showToast({
      icon:"none",
      title: '点击了Home',
    })
  },
  clickShare() {
    wx.showToast({
      icon: "none",
      title: '点击了分享',
    })
  },
  clickContact() {
    wx.showToast({
      icon: "none",
      title: '点击了联系',
    })
  },
  clickSave() {
    wx.showToast({
      icon: "none",
      title: '点击了保存',
    })
  }
})

wxml

  <view id="modal" animation="{{animationModal}}" style="position:fixed;width:80rpx;height:80rpx;z-index:1002;bottom: 100rpx;right: 30rpx;border-radius:40rpx;opacity: 0;background:#000"  bindtap="closeAnimate">
  </view>
  <image id="contact" style="position:fixed;width:80rpx;height:80rpx;z-index:1002;bottom: 100rpx;right: 30rpx;opacity: 0;" src="./contact.png" animation="{{animationContact}}" bindtap="contact" bindtap="clickContact"></image>
  <image id="share" style="position:fixed;width:80rpx;height:80rpx;z-index:1002;bottom: 100rpx;right: 30rpx;opacity: 0;" src="./share.png" animation="{{animationShare}}" bindtap="clickShare"></image>
  <image id="save" style="position:fixed;width:80rpx;height:80rpx;z-index:1002;bottom: 100rpx;right: 30rpx;opacity: 0;" src="./save.png" animation="{{animationSave}}" bindtap="clickSave"></image>
  <image id="home" style="position:fixed;width:80rpx;height:80rpx;z-index:1002;bottom: 100rpx;right: 30rpx;opacity: 0;" src="./home.png" animation="{{animationHome}}" bindtap="clickHome"></image>
  <image style="position:fixed;width:80rpx;height:80rpx;z-index:1002;bottom: 100rpx;right: 30rpx;" src="./float.png" bindtap="contorlAnimate"></image>

扩展:

这是实现本类问题的思路,其他诸如悬浮弹出之类的都可参照此操作,主要还是对动画的使用。

  • 1
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玩烂小程序

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值