本文编辑: DevelopeWhite浏览 5840
版权所有,严禁转载
Toast是当用户点击某些组件时弹出来来的提示消息。Toast会帮助你创建和显示这些消息。Toast是一种简易的消息提示框。当视图显示给用户,在应用程序中显示为浮动。小程序即将废弃Toast组件,故这里介绍Toast的API:wx.showToast
wxml
<!--index.wxml-->
<view class="content">
<text class="showfunc">Toast功能</text>
<view class="con-button">
<button class="button-left" bindtap="showToast">展示Toast</button>
<button class="button-right" bindtap="hideToast">隐藏Toast</button>
</view>
</view>
js
Page({
showToast:function(){
var that=this;
wx.showToast({
title: '成功',
icon: 'success',
duration: 2000,
success: that.flySuccess,
fail:that.flyFail,
complete:that.flyComplete
})
},
hideToast:function(){
var that=this;
wx.showToast({
title: '加载中',
icon: 'loading',
duration: 10000,
success: that.loadingSuccess,
fail:that.loadingFail,
complete:that.loadingComplete
});
setTimeout(function(){
wx.hideToast()
},2000)
},
flySuccess:function(e){
console.log(e);
console.log("起飞成功!");
},
flyFail:function(e){
console.log(e);
console.log("起飞失败!")
},
flyComplete:function(e){
console.log(e);
console.log("起飞结束!")
},
loadingSuccess:function(e){
console.log(e);
console.log("加载成功!");
},
loadingFail:function(e){
console.log(e);
console.log("加载失败!")
},
loadingComplete:function(e){
console.log(e);
console.log("加载结束!")
}
})
wxss
/**index.wxss**/
.con-button{
display: flex;
flex-direction: row;
padding-top: 10%;
}
.showfunc{
padding-top:10%;
display: block;
text-align: center;
color: green;
}
参数 | 类型 | 是否必填 | 参数描述 |
---|---|---|---|
title | String | 是 | 该提示框提示的内容 |
icon | String | 否 | 该提示框的图标,只支持”success”、”loading” |
duration | Number | 否 | 提示的延迟时间,单位毫秒,默认:1500, 最大为10000 |
success | Function | 否 | 该接口调用成功的回调函数 |
fail | Function | 否 | 接口调用失败的回调函数 |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |