当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 微信小程序实现弹框效果

微信小程序实现弹框效果

2020年06月14日  | 移动技术网IT编程  | 我要评论
本文实例为大家分享了微信小程序实现弹框效果的具体代码,供大家参考,具体内容如下先上代码wxml部分:<view class='top' bindtap='powerdrawer' data-st

本文实例为大家分享了微信小程序实现弹框效果的具体代码,供大家参考,具体内容如下

先上代码

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来完善这个动效。

为大家推荐现在关注度比较高的微信小程序教程一篇:《微信小程序开发教程》小编为大家精心整理的,希望喜欢。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网