微信小程序?qū)崙?zhàn)之自定義toast(6)
微信提供了一個(gè)toast的api wx.showToast()
相關(guān)連接:https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-react.html#wxshowtoastobject
本來(lái)是比較好的,方便使用,但是這個(gè)toast會(huì)顯示出圖標(biāo),而且不能去除。
假設(shè):我們執(zhí)行完業(yè)務(wù)的時(shí)候,toast一下,當(dāng)執(zhí)行成功的時(shí)候,效果還可以接受,如下圖:
但是,當(dāng)執(zhí)行失敗的時(shí)候,如下圖:
失敗了,你還顯示個(gè)圖案,那到底是成功還是失????這肯定是不能接受的。
若是給老板看到這種效果,又是一頓臭罵,程序猿的屈屈
下面介紹一個(gè)自定義的toast
效果:
具體實(shí)現(xiàn):
wxml:
<!--按鈕--> <view style="{{isShowToast?'position:fixed;':''}}"> <view class="btn" bindtap="clickBtn">button</view> </view> <!--mask--> <view class="toast_mask" wx:if="{{isShowToast}}"></view> <!--以下為toast顯示的內(nèi)容--> <view class="toast_content_box" wx:if="{{isShowToast}}"> <view class="toast_content"> <view class="toast_content_text"> {{toastText}} </view> </view> </view>
wxss:
Page { background: #fff; } /*按鈕*/ .btn { font-size: 28rpx; padding: 15rpx 30rpx; width: 100rpx; margin: 20rpx; text-align: center; border-radius: 10rpx; border: 1px solid #000; } /*mask*/ .toast_mask { opacity: 0; width: 100%; height: 100%; overflow: hidden; position: fixed; top: 0; left: 0; z-index: 888; } /*toast*/ .toast_content_box { display: flex; width: 100%; height: 100%; justify-content: center; align-items: center; position: fixed; z-index: 999; } .toast_content { width: 50%; padding: 20rpx; background: rgba(0, 0, 0, 0.5); border-radius: 20rpx; } .toast_content_text { height: 100%; width: 100%; color: #fff; font-size: 28rpx; text-align: center; }
js:
Page({ data: { //toast默認(rèn)不顯示 isShowToast: false }, showToast: function () { var _this = this; // toast時(shí)間 _this.data.count = parseInt(_this.data.count) ? parseInt(_this.data.count) : 3000; // 顯示toast _this.setData({ isShowToast: true, }); // 定時(shí)器關(guān)閉 setTimeout(function () { _this.setData({ isShowToast: false }); }, _this.data.count); }, /* 點(diǎn)擊按鈕 */ clickBtn: function () { console.log("你點(diǎn)擊了按鈕") //設(shè)置toast時(shí)間,toast內(nèi)容 this.setData({ count: 1500, toastText: 'Michael's Toast' }); this.showToast(); } })
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript中undefined與null的區(qū)別
在JavaScript中存在這樣兩種原始類型:Null與Undefined。這兩種類型常常會(huì)使JavaScript的開(kāi)發(fā)人員產(chǎn)生疑惑,在什么時(shí)候是Null,什么時(shí)候又是Undefined?2015-08-08簡(jiǎn)單實(shí)現(xiàn)JS倒計(jì)時(shí)效果
這篇文章主要教大家如何簡(jiǎn)單實(shí)現(xiàn)JS倒計(jì)時(shí)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12IE6下出現(xiàn)JavaScript未結(jié)束的字符串常量錯(cuò)誤的解決方法
JavaScript文件只在IE6下出錯(cuò)(“未結(jié)束的字符串常量”)的解決辦法。2010-11-11