重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
本文小編為大家詳細介紹“微信小程序開發中怎么使用toast等彈框提示”,內容詳細,步驟清晰,細節處理妥當,希望這篇“微信小程序開發中怎么使用toast等彈框提示”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
創新互聯公司專注于白銀區企業網站建設,響應式網站,商城建設。白銀區網站建設公司,為白銀區等地區提供建站服務。全流程按需設計,專業設計,全程項目跟蹤,創新互聯公司專業和態度為您提供的服務
介紹
微信小程序中toast消息提示框只有兩種顯示的效果,就是成功和加載,使用wx.showToast(OBJECT)
。
看下有關參數說明:
代碼很簡單:
wx.showToast({ title: '成功', icon: 'succes', duration: 1000, mask:true })
mask屬性好像并沒有起作用。有一點值得注意的是提示的延遲時間是有限制的,最大10000毫秒。
還有一個函數是wx.hideToast()
,這個是隱藏toast,主要用于顯示加載提示的時候用到,如:
wx.showToast({ title: '加載中', icon: 'loading', duration: 10000 }) setTimeout(function(){ wx.hideToast() },2000)
本來加載時間是10000毫秒的,然后2000毫秒的時候就隱藏了,這個具體情況而定了哈。
第二個彈窗是模態彈窗:wx.showModal(OBJECT)
參數如下:
這個跟我們Android里面的Dialog相似,效果如下:
代碼如下:
wx.showModal({ title: '提示', content: '模態彈窗', success: function (res) { if (res.confirm) { console.log('用戶點擊確定') }else{ console.log('用戶點擊取消') } } })
最后一個是操作菜單:wx.showActionSheet(OBJECT)
這個函數我們在上一篇文章用過,這里說一下也無妨。
先看一下參數介紹:
success有一個返回參數:
這里直接貼官方實例了:
wx.showActionSheet({ itemList: ['A', 'B', 'C'], success: function(res) { console.log(res.tapIndex) }, fail: function(res) { console.log(res.errMsg) } })
效果圖:
這里有個小問題,彈出showActionSheet之后,點擊取消或者陰影處,會執行完fail之后,繼續執行success函數,當然了,這里肯定有辦法解決的,success其實有兩個返回參數,除了tapIndex之外,還有一個就是cancle,cancle就是是否取消,返回一個boolean,在彈出這個框之后在success里面做個判斷,if (!res.cancel)
{做不取消的操作就行了}。當然了,你也可以自己來定義。
下面看個自定義彈窗的:
wxml:
{{title}}
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) }
啟用動畫來做,效果杠杠的,自己動手來試試。
也可以使用action-sheet來布局,如下:
{{item}} 取消
Page({ data: { actionSheetHidden: true, actionSheetItems: items }, actionSheetTap: function(e) { this.setData({ actionSheetHidden: !this.data.actionSheetHidden }) }, actionSheetChange: function(e) { this.setData({ actionSheetHidden: !this.data.actionSheetHidden }) } } })
讀到這里,這篇“微信小程序開發中怎么使用toast等彈框提示”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注創新互聯行業資訊頻道。