微信小程序自定義底部彈出框功能
本文實(shí)例為大家分享了微信小程序自定義底部彈出框的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)這么一個(gè)功能,點(diǎn)擊選項(xiàng)進(jìn)行選擇,效果是從底部彈出選項(xiàng)框(帶滑出動(dòng)畫(huà)),選擇了某項(xiàng)或者點(diǎn)擊其他地方,隱藏(帶滑出動(dòng)畫(huà))。效果圖如下:
可適用于任何場(chǎng)景,如普通選項(xiàng)(如圖)或者類(lèi)似商城小程序選擇商品屬性的彈出框。只需要把內(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)色框框起來(lái)的,可按需替換。

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;}
/*動(dòng)畫(huà)前初始位置*/
.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;
}
如果項(xiàng)目中,多個(gè)頁(yè)面使用了同樣效果彈出框,如下的代碼可以放到公共樣式文件app.wxss中。

3. js代碼
Page({
/**
* 頁(yè)面的初始數(shù)據(jù)
*/
data: {
optionList:['所有','選項(xiàng)1','選項(xiàng)2'],
value:'所有',
hideFlag: true,//true-隱藏 false-顯示
animationData: {},//
},
// 點(diǎn)擊選項(xiàng)
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)建動(dòng)畫(huà)實(shí)例
var animation = wx.createAnimation({
duration: 400,//動(dòng)畫(huà)的持續(xù)時(shí)間
timingFunction: 'ease',//動(dòng)畫(huà)的效果 默認(rèn)值是linear->勻速,ease->動(dòng)畫(huà)以低速開(kāi)始,然后加快,在結(jié)束前變慢
})
this.animation = animation; //將animation變量賦值給當(dāng)前動(dòng)畫(huà)
var time1 = setTimeout(function () {
that.slideIn();//調(diào)用動(dòng)畫(huà)--滑入
clearTimeout(time1);
time1 = null;
}, 100)
},
// 隱藏遮罩層
hideModal: function () {
var that = this;
var animation = wx.createAnimation({
duration: 400,//動(dòng)畫(huà)的持續(xù)時(shí)間 默認(rèn)400ms
timingFunction: 'ease',//動(dòng)畫(huà)的效果 默認(rèn)值是linear
})
this.animation = animation
that.slideDown();//調(diào)用動(dòng)畫(huà)--滑出
var time1 = setTimeout(function () {
that.setData({
hideFlag: true
})
clearTimeout(time1);
time1 = null;
}, 220)//先執(zhí)行下滑動(dòng)畫(huà),再隱藏模塊
},
//動(dòng)畫(huà) -- 滑入
slideIn: function () {
this.animation.translateY(0).step() // 在y軸偏移,然后用step()完成一個(gè)動(dòng)畫(huà)
this.setData({
//動(dòng)畫(huà)實(shí)例的export方法導(dǎo)出動(dòng)畫(huà)數(shù)據(jù)傳遞給組件的animation屬性
animationData: this.animation.export()
})
},
//動(dòng)畫(huà) -- 滑出
slideDown: function () {
this.animation.translateY(300).step()
this.setData({
animationData: this.animation.export(),
})
},
})
如上,用“// ------------------------------------------modal”隔開(kāi)的以下的代碼可不需要?jiǎng)印?/p>
如果一個(gè)頁(yè)面中使用了兩個(gè)同樣效果的彈出框,只需要稍微修改一下就行了,這里就不貼出來(lái)。
為大家推薦現(xiàn)在關(guān)注度比較高的微信小程序教程一篇:《微信小程序開(kāi)發(fā)教程》小編為大家精心整理的,希望喜歡。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript設(shè)計(jì)模式之單例模式實(shí)例
這篇文章主要介紹了JavaScript設(shè)計(jì)模式之單例模式實(shí)例,本文用一個(gè)實(shí)際例子講解JavaScript中的單例模式,需要的朋友可以參考下2014-09-09
關(guān)于JS前端實(shí)現(xiàn)水印的代碼操作
這篇文章主要介紹了關(guān)于JS前端實(shí)現(xiàn)水印的代碼操作,文中給出了詳細(xì)的實(shí)現(xiàn)思路和代碼示例供大家參考,對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-06-06
微信小程序 動(dòng)態(tài)綁定數(shù)據(jù)及動(dòng)態(tài)事件處理
這篇文章主要介紹了微信小程序 動(dòng)態(tài)綁定數(shù)據(jù)及動(dòng)態(tài)事件處理的相關(guān)資料,需要的朋友可以參考下2017-03-03
JavaScript獲取頁(yè)面中第一個(gè)錨定文本的方法
這篇文章主要介紹了JavaScript獲取頁(yè)面中第一個(gè)錨定文本的方法,涉及javascript操作document.archors數(shù)組的技巧,需要的朋友可以參考下2015-04-04
基于Bootstrap的后臺(tái)管理面板 Bootstrap Metro Dashboard
這篇文章主要介紹了基于Bootstrap的后臺(tái)管理面板:Bootstrap Metro Dashboard,對(duì)Bootstrap的后臺(tái)管理面板感興趣的小伙伴們可以參考一下2016-06-06
詳解JavaScript中扁平與樹(shù)形數(shù)據(jù)的轉(zhuǎn)換
這篇文章主要為大家想介紹了JavaScript中實(shí)現(xiàn)扁平與樹(shù)形數(shù)據(jù)相互轉(zhuǎn)換的方法,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)JavaScript有一定的幫助,需要的可以參考一下2023-01-01

