小程序自定义底部弹窗

小程序自定义底部弹窗

话不多说直接上代码

WXML部分

<view class="wrap" hidden="{{!visible}}">
  <view class="mask {{visible ? 'mask-show' : ''}}" hidden="{{!visible}}" bindtap="_onHidden"></view>
  <view class="box" id="my-modal-box" animation="{{animationData}}">
    <slot />
  </view>
</view>

下面中的isFooterBtn是在工作中一个特殊的样式,需要把底部按钮露出来不需要可以删除
JS部分

Component({
  properties: {
    myVisible: {
      type: Boolean,
      value: false,
      observer: '_visibleChange',
    },
    isFooterBtn: {
      type: Boolean,
      value: false,
    }
  },

  data: {
    visible: false,
    animation: null,
    animationData: null,
  },


  methods: {
    _visibleChange: function (newValue) {
      newValue ? this._onShow() : this._onHidden()
    },
	// 显示
    _onShow: function () {
      var animation = wx.createAnimation({
        duration: 400,
        timingFunction: "linear",
        delay: 0,
      })
      
      animation.translateY(500).step()
      this.setData({
        visible: true,
        animationData: animation.export(),
      })
      setTimeout(function () {
        animation.translateY(this.properties.isFooterBtn ? -56 : 0).step()
        this.setData({
          animationData: animation.export(),
        })
      }.bind(this), 400)
    },
	
	//隐藏
    _onHidden: function () {
      var animation = wx.createAnimation({
        duration: 400,
        timingFunction: "linear",
        delay: 0,
      })
      animation.translateY(500).step()
      this.setData({
        animationData: animation.export(),
      })
      setTimeout(function () {
        this.setData({
          visible: false
        })
        this.triggerEvent('myOnCancel');
      }.bind(this), 400)
    },
  },
})

CSS部分

.wrap {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 98;
  width: 100vw;
  height: 100vh;
}

.mask {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
  background: #000;
  opacity: 0;
}
.mask-show {
  opacity: 0.4;
}

.box {
  position: fixed;
  top: 100vh;
  left: 0;
  z-index: 2;
  width: 100%;
  min-height: 100rpx;
  background: #fff;
}

引入使用

json部分

"usingComponents": {
    "timeModal": "/component/my-model/index"
  }

wxml部分

<timeModal myVisible="{{timeVisible}}" bindmyOnCancel="timeHidden">
//里面写入你需要展示的样式
</timeModal>

js部分

data: {
    visible: false
  },
  
  // 时间显示
  timeShow: function () {
    this.setData({
      visible: true,
    })
  },

  // 时间隐藏
  timeHidden: function () {
    this.setData({
      visible: false
    });
  },

也是拿的网上的代码改成自己需要的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值