微信小程序之底部彈出模態(tài)框 完美解決

與其違心賠笑,不如一人安靜

在這里插入圖片描述

首先上wxml

<!-- 觸發(fā)按鈕 -->
<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">發(fā)送給朋友</button>
            <view class="btn" bind:tap="eventDraw">生成海報</view>
        </view>
        <view bindtap="hideModal" class="btn borderCl cancel">取消</view>
    </view>
</view>
// 顯示遮罩層
    Page({
  data: {
    hideModal: true, //模態(tài)框的狀態(tài)  true-隱藏  false-顯示
    animationData: {},
  },
 
  // 顯示遮罩層
  showModal: function () {
    var that = this;
    that.setData({
      hideModal: false
    })
    var animation = wx.createAnimation({
      duration: 600,//動畫的持續(xù)時間 默認600ms   數(shù)值越大,動畫越慢   數(shù)值越小,動畫越快
      timingFunction: 'ease',//動畫的效果 默認值是linear
    })
    this.animation = animation
    setTimeout(function () {
      that.fadeIn();//調(diào)用顯示動畫
    }, 200)
  },
 
  // 隱藏遮罩層
  hideModal: function () {
    var that = this;
    var animation = wx.createAnimation({
      duration: 800,//動畫的持續(xù)時間 默認800ms   數(shù)值越大,動畫越慢   數(shù)值越小,動畫越快
      timingFunction: 'ease',//動畫的效果 默認值是linear
    })
    this.animation = animation
    that.fadeDown();//調(diào)用隱藏動畫   
    setTimeout(function () {
      that.setData({
        hideModal: true
      })
    }, 720)//先執(zhí)行下滑動畫,再隱藏模塊
 
  },
 
  //動畫集
  fadeIn: function () {
    this.animation.translateY(0).step()
    this.setData({
      animationData: this.animation.export()//動畫實例的export方法導出動畫數(shù)據(jù)傳遞給組件的animation屬性
    })
  },
  fadeDown: function () {
    this.animation.translateY(300).step()
    this.setData({
      animationData: this.animation.export(),
    })
  },
})
/*模態(tài)框*/

.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: 李大玄

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容