微信小程序自定義toast組件的方法詳解【含動畫】
本文實(shí)例講述了微信小程序自定義toast組件的方法。分享給大家供大家參考,具體如下:
怎么創(chuàng)建就不說了,前面一篇有
微信小程序自定義prompt組件
直接上代碼
wxml
<!-- components/toast/toast.wxml --> <view class="toast-box {{isShow? 'show':''}}" animation="{{animationData}}"> <view class="toast-content" > <view class="toast-img"> <block wx:if="{{type==='success'}}"> <image class="toast-icon" src="xxx" /> </block> <block wx:if="{{type==='fail'}}"> <image class="toast-icon" src="xxx" /> </block> </view> <view class="toast-title">{{title}}</view> </view> </view>
js
// components/toast/toast.js Component({ properties: { }, data: { type: 'fail', title: '你還沒有勾選呢!', isShow: false, animationData: '' }, methods: { showToast: function (data) { const self = this; if (this._showTimer) { clearTimeout(this._showTimer) } if (this._animationTimer) { clearTimeout(this._animationTimer) } // display需要先設(shè)置為block之后,才能執(zhí)行動畫 this.setData({ title: data.title, type: data.type, isShow: true, }); this._animationTimer = setTimeout(() => { const animation = wx.createAnimation({ duration: 500, timingFunction: 'ease', delay: 0 }) animation.opacity(1).step(); self.setData({ animationData: animation.export(), }) }, 50) this._showTimer = setTimeout(function () { self.hideToast(); if (data.compelete && (typeof data.compelete === 'function')) { data.compelete() } }, 1200 || (50 + data.duration)) }, hideToast: function () { if (this._hideTimer) { clearTimeout(this._hideTimer) } let animation = wx.createAnimation({ duration: 200, timingFunction: 'ease', delay: 0 }) animation.opacity(0).step(); this.setData({ animationData: animation.export(), }) this._hideTimer = setTimeout(() => { this.setData({ isShow: false, }) }, 250) } } })
json
{ "component": true, "usingComponents": {} }
wxss
/* components/toast/toast.wxss */ .toast-box { position: absolute; left: 0; top: 0; width: 100%; height: 100%; z-index: 11; display: none; opacity: 0; } .show{ display: block; } .toast-content { position: absolute; left: 50%; top: 35%; width: 350rpx; /*height: 250rpx;*/ border-radius: 10rpx; box-sizing: bordre-box; transform: translate(-50%, -50%); background: rgba(0, 0, 0, .7); } .toast-img{ width: 100%; height: 120rpx; padding-top: 15rpx; box-sizing: bordre-box; text-align: center; } .toast-icon{ width: 100rpx; height: 100rpx; } .toast-title { width: 100%; padding:10rpx; line-height: 65rpx; color: white; text-align: center; font-size: 40rpx; box-sizing: border-box; }
使用
例如,在index.html中使用
在json中添加useComponents屬性
"usingComponents": { "vas-prompt": "./components/toast/toast" }
wxml
<vas-toast id='toast'></vas-toast> <button bindtap="showToast">點(diǎn)擊彈出toast</button>
js
//在onReady生命周期函數(shù)中,先獲取prompt實(shí)例 onReady:function(){ this.prompt = this.selectComponent("#toast"); }, showToast:function(){ this.toast.showToast({ type: 'success', title: '測試彈出消息', duration: 1000, compelete: function () { console.log('toast框隱藏之后,會調(diào)用該函數(shù)') //例如:跳轉(zhuǎn)頁面wx.navigateTo({ url: 'xxx' }); } }) },
效果
希望本文所述對大家微信小程序開發(fā)有所幫助。
相關(guān)文章
關(guān)于document.cookie的使用javascript
構(gòu)造通用的cookie處理函數(shù) cookie的處理過程比較復(fù)雜,并具有一定的相似性。因此可以定義幾個函數(shù)來完成cookie的通用操作,從而實(shí)現(xiàn)代碼的復(fù)用。2010-10-10ECMAScript6函數(shù)默認(rèn)參數(shù)
這篇文章主要介紹了ECMAScript6函數(shù)默認(rèn)參數(shù)的相關(guān)資料,需要的朋友可以參考下2015-06-06JavaScript實(shí)現(xiàn)的原生態(tài)Tab標(biāo)簽頁功能【兼容IE6】
這篇文章主要介紹了JavaScript實(shí)現(xiàn)的原生態(tài)Tab標(biāo)簽頁功能,可兼容IE6及谷歌等瀏覽器,涉及javascript事件響應(yīng)及頁面元素動態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-09-09詳解JavaScript兩個實(shí)用的圖片懶加載優(yōu)化方法
本文主要介紹了JavaScript兩個實(shí)用的圖片懶加載優(yōu)化方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03JavaScript實(shí)現(xiàn)各種排序的代碼詳解
這篇文章給大家介紹了js實(shí)現(xiàn)各種排序功能,包括冒泡排序,選擇排序和插入排序,代碼簡單易懂,非常不錯,具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-08-08JS根據(jù)key值獲取URL中的參數(shù)值及把URL的參數(shù)轉(zhuǎn)換成json對象
本篇文章主要圍繞js url 參數(shù)值展開話題,js根據(jù)key值獲取url中的參數(shù)值,接著把url的參數(shù)轉(zhuǎn)換成json,感興趣的朋友一起來學(xué)習(xí)吧,本文寫的不好地方還望多多指出批評建議2015-08-08js 數(shù)組隨機(jī)字符串(廣告不重復(fù))
今天一個網(wǎng)友想讓他的廣告隨機(jī)顯示,每次刷新廣告的內(nèi)容都不一樣,經(jīng)過參考源碼網(wǎng)站分析就是通過下面代碼實(shí)現(xiàn),特分享下方便需要的朋友2013-08-08微信小程序自定義菜單導(dǎo)航實(shí)現(xiàn)樓梯效果
在html開發(fā)中,我們可以用到a標(biāo)簽錨點(diǎn)實(shí)現(xiàn),jq的動畫相結(jié)合實(shí)現(xiàn)類似效果。在框架中vant UI框架也為我們實(shí)現(xiàn)了這一效果。接下來通過本文給大家介紹微信小程序自定義菜單導(dǎo)航實(shí)現(xiàn)樓梯效果,感興趣的朋友一起看看吧2021-12-12