微信小程序自定義底部彈出框功能
本文實例為大家分享了微信小程序自定義底部彈出框的具體代碼,供大家參考,具體內(nèi)容如下
實現(xiàn)這么一個功能,點擊選項進(jìn)行選擇,效果是從底部彈出選項框(帶滑出動畫),選擇了某項或者點擊其他地方,隱藏(帶滑出動畫)。效果圖如下:
可適用于任何場景,如普通選項(如圖)或者類似商城小程序選擇商品屬性的彈出框。只需要把內(nèi)容替換自己需要的即可。
1. wxml代碼
<view class="wrap"> <view bindtap="showModal"> <text>{{value}}</text> <icon class="arrow"></icon> </view> <!-- modal --> <view class="modal modal-bottom-dialog" hidden="{{hideFlag}}"> <view class="modal-cancel" bindtap="hideModal"></view> <view class="bottom-dialog-body bottom-positon" animation="{{animationData}}"> <!-- --> <view class='Mselect'> <view wx:for="{{optionList}}" wx:key="unique" data-value='{{item}}' bindtap='getOption'> {{item}} </view> </view> <view></view> <view class='Mcancel' bindtap='mCancel'> <text>取消</text> </view> </view> </view> </view>
modal中,藍(lán)色框框起來的,可按需替換。
2. wxss代碼
.arrow{ display:inline-block; border:6px solid transparent; border-top-color:#000; margin-left:8px; position:relative; top:6rpx; } /* ---------------------------- */ /*模態(tài)框*/ .modal{position:fixed; top:0; right:0; bottom:0; left:0; z-index:1000;} .modal-cancel{position:absolute; z-index:2000; top:0; right:0; bottom: 0; left:0; background:rgba(0,0,0,0.3);} .bottom-dialog-body{width:100%; position:absolute; z-index:3000; bottom:0; left:0;background:#dfdede;} /*動畫前初始位置*/ .bottom-positon{-webkit-transform:translateY(100%);transform:translateY(100%);} /* 底部彈出框 */ .bottom-positon{ text-align: center; } .Mselect{ margin-bottom: 20rpx; } .Mselect view{ padding: 26rpx 0; background: #fff; } .Mselect view:not(:last-of-type){ border-bottom: 1px solid #dfdede; } .Mcancel{ background: #fff; padding: 26rpx 0; }
如果項目中,多個頁面使用了同樣效果彈出框,如下的代碼可以放到公共樣式文件app.wxss中。
3. js代碼
Page({ /** * 頁面的初始數(shù)據(jù) */ data: { optionList:['所有','選項1','選項2'], value:'所有', hideFlag: true,//true-隱藏 false-顯示 animationData: {},// }, // 點擊選項 getOption:function(e){ var that = this; that.setData({ value:e.currentTarget.dataset.value, hideFlag: true }) }, //取消 mCancel: function () { var that = this; that.hideModal(); }, // ----------------------------------------------------------------------modal // 顯示遮罩層 showModal: function () { var that = this; that.setData({ hideFlag: false }) // 創(chuàng)建動畫實例 var animation = wx.createAnimation({ duration: 400,//動畫的持續(xù)時間 timingFunction: 'ease',//動畫的效果 默認(rèn)值是linear->勻速,ease->動畫以低速開始,然后加快,在結(jié)束前變慢 }) this.animation = animation; //將animation變量賦值給當(dāng)前動畫 var time1 = setTimeout(function () { that.slideIn();//調(diào)用動畫--滑入 clearTimeout(time1); time1 = null; }, 100) }, // 隱藏遮罩層 hideModal: function () { var that = this; var animation = wx.createAnimation({ duration: 400,//動畫的持續(xù)時間 默認(rèn)400ms timingFunction: 'ease',//動畫的效果 默認(rèn)值是linear }) this.animation = animation that.slideDown();//調(diào)用動畫--滑出 var time1 = setTimeout(function () { that.setData({ hideFlag: true }) clearTimeout(time1); time1 = null; }, 220)//先執(zhí)行下滑動畫,再隱藏模塊 }, //動畫 -- 滑入 slideIn: function () { this.animation.translateY(0).step() // 在y軸偏移,然后用step()完成一個動畫 this.setData({ //動畫實例的export方法導(dǎo)出動畫數(shù)據(jù)傳遞給組件的animation屬性 animationData: this.animation.export() }) }, //動畫 -- 滑出 slideDown: function () { this.animation.translateY(300).step() this.setData({ animationData: this.animation.export(), }) }, })
如上,用“// ------------------------------------------modal”隔開的以下的代碼可不需要動。
如果一個頁面中使用了兩個同樣效果的彈出框,只需要稍微修改一下就行了,這里就不貼出來。
為大家推薦現(xiàn)在關(guān)注度比較高的微信小程序教程一篇:《微信小程序開發(fā)教程》小編為大家精心整理的,希望喜歡。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
微信小程序 動態(tài)綁定數(shù)據(jù)及動態(tài)事件處理
這篇文章主要介紹了微信小程序 動態(tài)綁定數(shù)據(jù)及動態(tài)事件處理的相關(guān)資料,需要的朋友可以參考下2017-03-03基于Bootstrap的后臺管理面板 Bootstrap Metro Dashboard
這篇文章主要介紹了基于Bootstrap的后臺管理面板:Bootstrap Metro Dashboard,對Bootstrap的后臺管理面板感興趣的小伙伴們可以參考一下2016-06-06詳解JavaScript中扁平與樹形數(shù)據(jù)的轉(zhuǎn)換
這篇文章主要為大家想介紹了JavaScript中實現(xiàn)扁平與樹形數(shù)據(jù)相互轉(zhuǎn)換的方法,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)JavaScript有一定的幫助,需要的可以參考一下2023-01-01