微信小程序自定义底部弹框

该弹框自底部向上滑动弹出,同时蒙板添加透明的动画,点击蒙版,弹框下滑,蒙板渐变消失

wxml 

<view class="modals">
    <view class="mask" bindtap="hideModal" animation="{{animationMask}}"></view>
    <view class="content bottom-pos" animation="{{animationData}}">布局内容</view>
</view>

<button class='btn' bindtap="showModal">点我</button>

wxss

/*模态框*/
.btn{
  z-index: 1002
}
.modals {
  position: fixed;
  z-index: 999;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.mask {
  position: absolute;
  z-index: 1000;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  opacity: 0
}

.content {
  position: absolute;
  z-index: 1001;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 30rpx;
  height: 300rpx;
  background-color: #fff;
}

/*动画前初始位置*/

.bottom-pos {
  -webkit-transform: translateY(100%);
  transform: translateY(100%);
}

 

js 

Page({
  data: {
    animationData: {}, //内容动画
    animationMask: {} //蒙板动画
  },
  onLoad: function() {
    this.animateTrans = wx.createAnimation({
      duration: 600,
      timingFunction: 'ease',
    })

    this.animateFade = wx.createAnimation({
      duration: 600,
      timingFunction: 'ease',
    })
  },

  // 显示
  showModal: function() {
    this.animateTrans.translateY(0).step()
    this.animateFade.opacity(1).step()
    this.setData({
      animationData: this.animateTrans.export(), //动画实例的export方法导出动画数据传递给组件的animation属性
      animationMask: this.animateFade.export()
    })
  },

  // 隐藏
  hideModal: function() {
    this.animateTrans.translateY(300).step()
    this.animateFade.opacity(0).step()
    this.setData({
      animationData: this.animateTrans.export(),
      animationMask: this.animateFade.export()
    })
  }

})

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值