微信小程序之底部弹出模态框 完美解决

与其违心赔笑,不如一人安静

在这里插入图片描述

首先上wxml

<!-- 触发按钮 -->
<view class="shareButtonBox" bindtap="showModal">
    <image class="shareButton" src="/images/shareButton.png"></image>
    <view class="shareTips">分享</view>
</view>
<view class="modals modals-bottom-dialog">
    <view class="modals-cancel" bindtap="hideModal"></view>
    <view class="bottom-dialog-body bottom-pos" animation="{{animationData}}">
        <view class="borderCl">
            <button class="btn sendFriends shareTips" open-type="share">发送给朋友</button>
            <view class="btn" bind:tap="eventDraw">生成海报</view>
        </view>
        <view bindtap="hideModal" class="btn borderCl cancel">取消</view>
    </view>
</view>
// 显示遮罩层
    Page({
  data: {
    hideModal: true, //模态框的状态  true-隐藏  false-显示
    animationData: {},
  },
 
  // 显示遮罩层
  showModal: function () {
    var that = this;
    that.setData({
      hideModal: false
    })
    var animation = wx.createAnimation({
      duration: 600,//动画的持续时间 默认600ms   数值越大,动画越慢   数值越小,动画越快
      timingFunction: 'ease',//动画的效果 默认值是linear
    })
    this.animation = animation
    setTimeout(function () {
      that.fadeIn();//调用显示动画
    }, 200)
  },
 
  // 隐藏遮罩层
  hideModal: function () {
    var that = this;
    var animation = wx.createAnimation({
      duration: 800,//动画的持续时间 默认800ms   数值越大,动画越慢   数值越小,动画越快
      timingFunction: 'ease',//动画的效果 默认值是linear
    })
    this.animation = animation
    that.fadeDown();//调用隐藏动画   
    setTimeout(function () {
      that.setData({
        hideModal: true
      })
    }, 720)//先执行下滑动画,再隐藏模块
 
  },
 
  //动画集
  fadeIn: function () {
    this.animation.translateY(0).step()
    this.setData({
      animationData: this.animation.export()//动画实例的export方法导出动画数据传递给组件的animation属性
    })
  },
  fadeDown: function () {
    this.animation.translateY(300).step()
    this.setData({
      animationData: this.animation.export(),
    })
  },
})
/*模态框*/

.modals {
    position: fixed;
    z-index: 999;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

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

.modals .bottom-dialog-body {
    position: absolute;
    z-index: 10001;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 30rpx;
    height: 300rpx;
    /* background-color: #fff; */
}

/*动画前初始位置*/

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

.modals .btn {
    background: rgba(255, 255, 255, 0.82);
    color: #333;
    display: flex;
    flex-direction: column;
    align-items: center;
    height: 97rpx;
    justify-content: center;
    font-size: 34rpx;
    border-radius: 0;
}

.modals .sendFriends {
    border-bottom: 1rpx solid #acacac;
}

.modals .borderCl {
    border-radius: 12rpx;
    overflow: hidden;
}

.modals .cancel {
    margin: 16rpx 0;
}

在最后附上我的QQ: 2489757828 有问题的话可以找我一同探讨

我的私人博客: 李大玄
我的CSDN: 李大玄

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容