微信小程序開發(fā)之toast等彈框提示使用教程
介紹
微信小程序中toast消息提示框只有兩種顯示的效果,就是成功和加載,使用wx.showToast(OBJECT) 。
看下有關(guān)參數(shù)說明:

代碼很簡單:
wx.showToast({
title: '成功',
icon: 'succes',
duration: 1000,
mask:true
})

mask屬性好像并沒有起作用。有一點(diǎn)值得注意的是提示的延遲時(shí)間是有限制的,最大10000毫秒。
還有一個(gè)函數(shù)是wx.hideToast() ,這個(gè)是隱藏toast,主要用于顯示加載提示的時(shí)候用到,如:
wx.showToast({
title: '加載中',
icon: 'loading',
duration: 10000
})
setTimeout(function(){
wx.hideToast()
},2000)
本來加載時(shí)間是10000毫秒的,然后2000毫秒的時(shí)候就隱藏了,這個(gè)具體情況而定了哈。
第二個(gè)彈窗是模態(tài)彈窗:wx.showModal(OBJECT)
參數(shù)如下:

這個(gè)跟我們Android里面的Dialog相似,效果如下:

代碼如下:
wx.showModal({
title: '提示',
content: '模態(tài)彈窗',
success: function (res) {
if (res.confirm) {
console.log('用戶點(diǎn)擊確定')
}else{
console.log('用戶點(diǎn)擊取消')
}
}
})
最后一個(gè)是操作菜單:wx.showActionSheet(OBJECT)
這個(gè)函數(shù)我們?cè)?a target="_blank" href="http://www.dhdzp.com/article/106429.htm">上一篇文章用過,這里說一下也無妨。
先看一下參數(shù)介紹:

success有一個(gè)返回參數(shù):

這里直接貼官方實(shí)例了:
wx.showActionSheet({
itemList: ['A', 'B', 'C'],
success: function(res) {
console.log(res.tapIndex)
},
fail: function(res) {
console.log(res.errMsg)
}
})
效果圖:

這里有個(gè)小問題,彈出showActionSheet之后,點(diǎn)擊取消或者陰影處,會(huì)執(zhí)行完fail之后,繼續(xù)執(zhí)行success函數(shù),當(dāng)然了,這里肯定有辦法解決的,success其實(shí)有兩個(gè)返回參數(shù),除了tapIndex之外,還有一個(gè)就是cancle,cancle就是是否取消,返回一個(gè)boolean,在彈出這個(gè)框之后在success里面做個(gè)判斷,if (!res.cancel) {做不取消的操作就行了}。當(dāng)然了,你也可以自己來定義。
下面看個(gè)自定義彈窗的:
wxml:
<view class="commodity_screen" bindtap="hideModal" wx:if="{{showModalStatus}}"></view>
<view animation="{{animationData}}" class="commodity_attr_box" wx:if="{{showModalStatus}}" bindtap="navigate">
<text class="title">{{title}}</text>
</view>
css:
.commodity_screen {
width: 100%;
height: 100%;
position: fixed;
top: 0;
left: 0;
background: #000;
opacity: 0.2;
overflow: hidden;
z-index: 1000;
color: #fff;
}
.commodity_attr_box {
width: 100%;
overflow: hidden;
position: fixed;
bottom: 0;
left: 0;
z-index: 2000;
height: 60px;
background: #fff;
}
.title {
height: 100%;
width: 100%;
position: fixed;
text-align: center;
margin-top: 20px;
margin-bottom: 20px;
}
js:
showView() {
// 顯示遮罩層
var animation = wx.createAnimation({
duration: 200,
timingFunction: "linear",
delay: 0
})
this.animation = animation
animation.translateY(300).step()
this.setData({
animationData: animation.export(),
showModalStatus: true
})
setTimeout(function () {
animation.translateY(0).step()
this.setData({
animationData: animation.export()
})
}.bind(this), 200)
},
hideModal: function () {
this.hideView();
},
hideView() {
// 隱藏遮罩層
var animation = wx.createAnimation({
duration: 200,
timingFunction: "linear",
delay: 0
})
this.animation = animation
animation.translateY(300).step()
this.setData({
animationData: animation.export(),
})
setTimeout(function () {
animation.translateY(0).step()
this.setData({
animationData: animation.export(),
showModalStatus: false
})
}.bind(this), 200)
}
啟用動(dòng)畫來做,效果杠杠的,自己動(dòng)手來試試。
也可以使用action-sheet來布局,如下:
<action-sheet hidden="{{actionSheetHidden}}" bindchange="actionSheetChange">
<block wx:for-items="{{actionSheetItems}}">
<action-sheet-item class="item" bindtap="bind{{item}}">{{item}}</action-sheet-item>
</block>
<action-sheet-cancel class="cancel">取消</action-sheet-cancel>
</action-sheet>
Page({
data: {
actionSheetHidden: true,
actionSheetItems: items
},
actionSheetTap: function(e) {
this.setData({
actionSheetHidden: !this.data.actionSheetHidden
})
},
actionSheetChange: function(e) {
this.setData({
actionSheetHidden: !this.data.actionSheetHidden
})
}
}
})
就是這么簡單,趕緊動(dòng)起來試試吧。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
JavaScript實(shí)現(xiàn)隨機(jī)碼的生成與校驗(yàn)
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)隨機(jī)碼的生成與校驗(yàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
JavaScript數(shù)組排序功能簡單實(shí)現(xiàn)
這篇文章主要介紹了JavaScript數(shù)組排序功能簡單實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
js模擬如何實(shí)現(xiàn)重載以及默認(rèn)參數(shù)
這篇文章主要介紹了js模擬如何實(shí)現(xiàn)重載以及默認(rèn)參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
javascript setTimeout和setInterval計(jì)時(shí)的區(qū)別詳解
window對(duì)象有兩個(gè)主要的定時(shí)方法,分別是setTimeout 和 setInteval 他們的語法基本上相同,但是完成的功能取有區(qū)別。2013-06-06
JavaScript格式化數(shù)字的函數(shù)代碼
當(dāng)要格式化的數(shù)字為null、空或非數(shù)字時(shí),返回的結(jié)果。默認(rèn)為02010-11-11
JS實(shí)現(xiàn)讓訪問者自助選擇網(wǎng)頁文字顏色的方法
這篇文章主要介紹了JS實(shí)現(xiàn)讓訪問者自助選擇網(wǎng)頁文字顏色的方法,涉及javascript針對(duì)radio表單控件的操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-02-02
iis6+javascript Add an Extension File
iis6+javascript Add an Extension File...2007-06-06
echarts實(shí)現(xiàn)3d柱狀圖的2種方式舉例
echarts3D效果柱狀圖的實(shí)現(xiàn),這個(gè)太難了,我花了兩天終于調(diào)成我想要的效果啦,要是官網(wǎng)上有例子就好了,太難調(diào)了,下面這篇文章主要給大家介紹了關(guān)于echarts實(shí)現(xiàn)3d柱狀圖的2種方式,需要的朋友可以參考下2023-02-02

