小程序模板網(wǎng)

michael:微信小程序之自定義模態(tài)彈窗(帶動畫)實例

發(fā)布時間:2017-12-06 17:00 所屬欄目:小程序開發(fā)教程

首先看看官方提供的模態(tài)彈窗

 

api如下:


 

示例:


這樣的模態(tài)彈窗,充其量只能做個alert,提示一下信息。

但是并不能使用它來處理復(fù)雜性的彈窗業(yè)務(wù),因此寫了Michael從新自定義了一個,采用了仿原生的樣式寫法

 

 

wxml

 

[html] view plain copy
 
  1. <!--button-->  
  2. <view class="btn" bindtap="powerDrawer" data-statu="open">button</view>  
  3.   
  4. <!--mask-->  
  5. <view class="drawer_screen" bindtap="powerDrawer" data-statu="close" wx:if="{{showModalStatus}}"></view>  
  6. <!--content-->  
  7. <!--使用animation屬性指定需要執(zhí)行的動畫-->  
  8. <view animation="{{animationData}}" class="drawer_box" wx:if="{{showModalStatus}}">  
  9.   
  10.   <!--drawer content-->  
  11.   <view class="drawer_title">彈窗標(biāo)題</view>  
  12.   <view class="drawer_content">  
  13.     <view class="top grid">  
  14.       <label class="title col-0">標(biāo)題</label>  
  15.       <input class="input_base input_h30 col-1" name="rName" value="可自行定義內(nèi)容"></input>  
  16.     </view>  
  17.     <view class="top grid">  
  18.       <label class="title col-0">標(biāo)題</label>  
  19.       <input class="input_base input_h30 col-1" name="mobile" value="110"></input>  
  20.     </view>  
  21.     <view class="top grid">  
  22.       <label class="title col-0">標(biāo)題</label>  
  23.       <input class="input_base input_h30 col-1" name="phone" value="拒絕伸手黨"></input>  
  24.     </view>  
  25.     <view class="top grid">  
  26.       <label class="title col-0">標(biāo)題</label>  
  27.       <input class="input_base input_h30 col-1" name="Email" value="僅供學(xué)習(xí)使用"></input>  
  28.     </view>  
  29.     <view class="top bottom grid">  
  30.       <label class="title col-0">備注</label>  
  31.       <input class="input_base input_h30 col-1" name="bz"></input>  
  32.     </view>  
  33.   </view>  
  34.   <view class="btn_ok" bindtap="powerDrawer" data-statu="close">確定</view>  
  35. </view>  

 

 

 

 

wxss

 

[css] view plain copy
 
  1. /*button*/  
  2. .btn {  
  3.   width80%;  
  4.   padding20rpx 0;  
  5.   border-radius: 10rpx;  
  6.   text-aligncenter;  
  7.   margin40rpx 10%;  
  8.   background#000;  
  9.   color#fff;  
  10. }  
  11.   
  12. /*mask*/  
  13. .drawer_screen {  
  14.   width100%;  
  15.   height100%;  
  16.   positionfixed;  
  17.   top: 0;  
  18.   left: 0;  
  19.   z-index1000;  
  20.   background#000;  
  21.   opacity: 0.5;  
  22.   overflowhidden;  
  23. }  
  24.   
  25. /*content*/  
  26. .drawer_box {  
  27.   width650rpx;  
  28.   overflowhidden;  
  29.   positionfixed;  
  30.   top: 50%;  
  31.   left: 0;  
  32.   z-index1001;  
  33.   background#FAFAFA;  
  34.   margin-150px 50rpx 0 50rpx;  
  35.   border-radius: 3px;  
  36. }  
  37.   
  38. .drawer_title{  
  39.   padding:15px;  
  40.   font20px "microsoft yahei";  
  41.   text-aligncenter;  
  42. }  
  43. .drawer_content {  
  44.   height210px;  
  45.   overflow-y: scroll/*超出父盒子高度可滾動*/  
  46. }  
  47.   
  48. .btn_ok{  
  49.   padding10px;  
  50.   font20px "microsoft yahei";  
  51.   text-aligncenter;  
  52.   border-top1px solid #E8E8EA;  
  53.   color#3CC51F;  
  54. }  
  55.   
  56. .top{  
  57.     padding-top:8px;  
  58. }  
  59. .bottom {  
  60.     padding-bottom:8px;  
  61. }  
  62. .title {  
  63.     height30px;  
  64.     line-height30px;  
  65.     width160rpx;  
  66.     text-aligncenter;  
  67.     display: inline-block;  
  68.     font300 28rpx/30px "microsoft yahei";  
  69. }  
  70.   
  71. .input_base {  
  72.     border2rpx solid #ccc;  
  73.     padding-left10rpx;  
  74.     margin-right50rpx;  
  75. }  
  76. .input_h30{  
  77.     height30px;  
  78.     line-height30px;  
  79. }  
  80. .input_h60{  
  81.     height60px;  
  82. }  
  83. .input_view{  
  84.     font12px "microsoft yahei";  
  85.     background#fff;  
  86.     color:#000;  
  87.     line-height30px;  
  88. }  
  89.   
  90. input {  
  91.     font12px "microsoft yahei";  
  92.     background#fff;  
  93.     color:#000 ;  
  94. }  
  95. radio{  
  96.     margin-right20px;  
  97. }  
  98. .grid { display: -webkit-box; display: box; }  
  99. .col-0 {-webkit-box-flex:0;box-flex:0;}  
  100. .col-1 {-webkit-box-flex:1;box-flex:1;}  
  101. .fl { floatleft;}  
  102. .fr { floatright;}  

 

 

js

[javascript] view plain copy
 
  1. Page({  
  2.   data: {  
  3.     showModalStatus: false  
  4.   },  
  5.   powerDrawer: function (e) {  
  6.     var currentStatu = e.currentTarget.dataset.statu;  
  7.     this.util(currentStatu)  
  8.   },  
  9.   util: function(currentStatu){  
  10.     /* 動畫部分 */  
  11.     // 第1步:創(chuàng)建動畫實例   
  12.     var animation = wx.createAnimation({  
  13.       duration: 200,  //動畫時長  
  14.       timingFunction: "linear"//線性  
  15.       delay: 0  //0則不延遲  
  16.     });  
  17.       
  18.     // 第2步:這個動畫實例賦給當(dāng)前的動畫實例  
  19.     this.animation = animation;  
  20.   
  21.     // 第3步:執(zhí)行第一組動畫  
  22.     animation.opacity(0).rotateX(-100).step();  
  23.   
  24.     // 第4步:導(dǎo)出動畫對象賦給數(shù)據(jù)對象儲存  
  25.     this.setData({  
  26.       animationData: animation.export()  
  27.     })  
  28.       
  29.     // 第5步:設(shè)置定時器到指定時候后,執(zhí)行第二組動畫  
  30.     setTimeout(function () {  
  31.       // 執(zhí)行第二組動畫  
  32.       animation.opacity(1).rotateX(0).step();  
  33.       // 給數(shù)據(jù)對象儲存的第一組動畫,更替為執(zhí)行完第二組動畫的動畫對象  
  34.       this.setData({  
  35.         animationData: animation  
  36.       })  
  37.         
  38.       //關(guān)閉  
  39.       if (currentStatu == "close") {  
  40.         this.setData(  
  41.           {  
  42.             showModalStatus: false  
  43.           }  
  44.         );  
  45.       }  
  46.     }.bind(this), 200)  
  47.     
  48.     // 顯示  
  49.     if (currentStatu == "open") {  
  50.       this.setData(  
  51.         {  
  52.           showModalStatus: true  
  53.         }  
  54.       );  
  55.     }  
  56.   }  
  57.   
  58. })  

運行:


易優(yōu)小程序(企業(yè)版)+靈活api+前后代碼開源 碼云倉庫:starfork
本文地址:http://www.u-renovate.com/wxmini/doc/course/18053.html 復(fù)制鏈接 如需定制請聯(lián)系易優(yōu)客服咨詢:800182392 點擊咨詢
QQ在線咨詢
AI智能客服 ×