微信小程序弹框效果解析

先上代码
wxml部分:

<view class='top' bindtap='powerDrawer' data-statu="open" data-num='300'>
  <text>向上弹起</text>
</view>

<view class='top' bindtap='powerDrawer' data-statu="open" data-num='-300'>
  <text>向下弹出</text>
</view>



<!--遮罩部分-->
<view class="drawer_screen" wx:if="{{showModalStatus}}" bindtap="powerDrawer" data-statu="close"></view> 
<!--弹出层内容-->
<!--使用animation属性指定需要执行的动画-->
<view animation="{{animationData}}" class="drawer_box" wx:if="{{showModalStatus}}"> 
   <view class='modalBox'>
    <view class='modalQues'>是否退出?</view>
    <view class='modalConf'>是否确定退出</view>
    <view class='hidePick'>
      <text bindtap="powerDrawer" data-statu="close" class='hideModal' >确定</text>
      <text bindtap="powerDrawer" data-statu="close" class='hideModal' >取消</text>
    </view>
   </view>
</view>

wxss部分:


.top {
  margin: 0 auto;
  margin-top: 50rpx;
  background: #1da0ee;
  color: #fff;
  width: 50vw;
  text-align: center
}

.drawer_screen { 
 width: 100%; 
 height: 100%; 
 position: fixed; 
 top: 0; 
 left: 0; 
 z-index: 1000; 
 background: #000; 
 opacity: 0.5; 
 overflow: hidden; 
} 
  
/*content*/
.drawer_box { 
  width:600rpx;
  height:300rpx;
  overflow:hidden;
  position:fixed;
  top:50%;
  left:50%;
  z-index:1001;
  background:#FAFAFA;
  margin-top:-150rpx;
  border-radius:3px;
  margin-left:-300rpx;
} 

.modalBox {
  padding: 60rpx;
  font-size: 30rpx;
}


.modalConf {
  font-size: 24rpx;
  color: #999;
  margin-top: 20rpx; 
}

.hidePick {
  text-align: right;
  margin-top: 50rpx;
}

.hideModal {
  color: #1da0ee;
  margin-left: 130rpx;
}

js部分:

Page({
  data: {

  },
  // 自定义弹框
  powerDrawer: function (e) {
    console.log(e) //打印出当前对象
    var currentStatu = e.currentTarget.dataset.statu; //获取statu属性值
    var currentNum = e.currentTarget.dataset.num;//获取num属性值
    currentNum = parseInt(currentNum , 10) //注意,这一步是将字符串转换为数字
    this.util(currentStatu,currentNum) //将参数引入util方法
  },
  util: function (currentStatu,currentNum) {
    /* 动画部分 */
    // 第1步:创建动画实例 
    var animation = wx.createAnimation({
      duration: 200, //动画时长 
      timingFunction: "linear", //线性 
      delay: 0 //0则不延迟 
    });

    // 第2步:这个动画实例赋给当前的动画实例 
    this.animation = animation;
    console.log(currentNum)
    // 第3步:执行第一组动画 
    animation.opacity(0).translateY(currentNum).step();

    // 第4步:导出动画对象赋给数据对象储存 
    this.setData({
      animationData: animation.export()
    })

    // 第5步:设置定时器到指定时候后,执行第二组动画 
    setTimeout(function () {
      // 执行第二组动画 
      animation.opacity(1).translateY(0).step();
      // 给数据对象储存的第一组动画,更替为执行完第二组动画的动画对象 
      this.setData({
        animationData: animation
      })

      //关闭 
      if (currentStatu == "close") {
        this.setData(
          {
            showModalStatus: false
          }
        );
      }
    }.bind(this), 200)

    // 显示 
    if (currentStatu == "open") {
      this.setData(
        {
          showModalStatus: true
        }
      );
    }
  },
})

这只是很简单的一个弹框 , 类似的左右弹出只需要将translateY改为translateX就行了。 但是这段代码有一个问题就是当你点击关闭的时候 , currentNum是不存在的,同时关闭弹框时currentNum我们不可以赋值 , 所以需要利用小程序的缓存APi来完善这个动效

猜你喜欢

转载自blog.csdn.net/weixin_42794596/article/details/82979533
今日推荐