微信小程序中的各种弹窗API

  • wx.showToast(OBJECT)


显示消息提示框。

OBJECT参数说明:

参数类型必填说明最低版本
titleString提示的内容 
iconString图标,有效值"success"、"loading" 
imageString自定义图标的本地路径,image 的优先级高于 icon1.1.0
durationNumber提示的延迟时间,单位毫秒,默认:1500 
maskBoolean是否显示透明蒙层,防止触摸穿透,默认:false 
successFunction接口调用成功的回调函数 
failFunction接口调用失败的回调函数 
completeFunction接口调用结束的回调函数(调用成功、失败都会执行) 

 

示例代码:

wx.showToast({
  title: '成功',
  icon: 'success',
  duration: 2000
})
  • wx.showLoading(OBJECT)


基础库版本 1.1.0 开始支持,低版本需做兼容处理

显示 loading 提示框, 需主动调用 wx.hideLoading 才能关闭提示框

OBJECT参数说明:

参数类型必填说明
titleString提示的内容
maskBoolean是否显示透明蒙层,防止触摸穿透,默认:false
successFunction接口调用成功的回调函数
failFunction接口调用失败的回调函数
completeFunction接口调用结束的回调函数(调用成功、失败都会执行)
  • wx.hideToast()


隐藏消息提示框

  • wx.hideLoading()


基础库版本 1.1.0 开始支持,低版本需做兼容处理

隐藏loading提示框

wx.showLoading({
  title: '加载中',
})

setTimeout(function(){
  wx.hideLoading()
},2000)
  • wx.showModal(OBJECT)


​显示模态弹窗

OBJECT参数说明:

参数类型必填说明
titleString提示的标题
contentString提示的内容
showCancelBoolean是否显示取消按钮,默认为 true
cancelTextString取消按钮的文字,默认为"取消",最多 4 个字符
cancelColorHexColor取消按钮的文字颜色,默认为"#000000"
confirmTextString确定按钮的文字,默认为"确定",最多 4 个字符
confirmColorHexColor确定按钮的文字颜色,默认为"#3CC51F"
successFunction接口调用成功的回调函数
failFunction接口调用失败的回调函数
completeFunction接口调用结束的回调函数(调用成功、失败都会执行)

succes返回参数说明:

参数类型说明最低版本
confirmBoolean为 true 时,表示用户点击了确定按钮 
cancelBoolean为 true 时,表示用户点击了取消(用于 Android 系统区分点击蒙层关闭还是点击取消按钮关闭)1.1.0

示例代码:

wx.showModal({
  title: '提示',
  content: '这是一个模态弹窗',
  success: function(res) {
    if (res.confirm) {
      console.log('用户点击确定')
    } else if (res.cancel) {
      console.log('用户点击取消')
    }
  }
})
  • wx.showActionSheet(OBJECT)


​显示操作菜单

OBJECT参数说明:

参数类型必填说明
itemListString Array按钮的文字数组,数组长度最大为6个
itemColorHexColor按钮的文字颜色,默认为"#000000"
successFunction接口调用成功的回调函数,详见返回参数说明
failFunction接口调用失败的回调函数
completeFunction接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数类型说明
tapIndexNumber用户点击的按钮,从上到下的顺序,从0开始

示例代码:

wx.showActionSheet({
  itemList: ['A', 'B', 'C'],
  success: function(res) {
    console.log(res.tapIndex)
  },
  fail: function(res) {
    console.log(res.errMsg)
  }
})

Bug & Tip

  1. bug:Android6.3.30,wx.showModal 的返回的 confirm 一直为 true;
  2. tip: wx.showActionSheet 点击取消或蒙层时,回调fail, errMsg 为 "showActionSheet:fail cancel";
  3. tip: wx.showLoading 和 wx.showToast 同时只能显示一个,使用 wx.hideToast/wx.hideLoading 都可以关闭提示框;
  4. tipiOS wx.showModal 点击蒙层不会关闭模态弹窗,所以尽量避免使用“取消”分支中实现业务逻辑。

 

<!-- test.wxml -->
<view class="content">
    <button bindtap="popSuccessTest">成功提示弹窗</button>
    <button bindtap="popMaskTest">带透明蒙层的弹窗</button>
    <button bindtap="popTest">纯文字弹窗</button>
    <button bindtap="popCustomIcon">自定义图标</button>
    <button bindtap="popConfirm">confirm的弹窗</button>
    <button bindtap="popLoading">加载弹窗</button>
    <button bindtap="popSelect">选择框弹窗</button>
</view>
//test.js
Page({
  popSuccessTest: function () {
    wx.showToast({
      title: '成功提示弹窗',
      icon: '',     //默认值是success,就算没有icon这个值,就算有其他值最终也显示success
      duration: 2000,      //停留时间
    })
  },
  popMaskTest: function () {
    wx.showToast({
      title: '带蒙层的弹窗',     
      duration: 2000,    
      mask:true    //是否有透明蒙层,默认为false 
                   //如果有透明蒙层,弹窗的期间不能点击文档内容 
    })
  },
  popTest: function(){
    wx.showToast({
      title: '纯文字弹窗',
      icon: 'none',    //如果要纯文本,不要icon,将值设为'none'
      duration: 2000     
    })  
  },
  popCustomIcon: function () {
    wx.showToast({
      title: '自定义图标弹窗',
      image: '../../static/image/icon.png',  //image的优先级会高于icon
      duration: 2000     
    })
  },
  popConfirm: function(){
    wx.showModal({
      title: 'confirm的弹窗',
      content: '确认要删除该项吗?',
      success: function (res) {
        if (res.confirm) {  
          console.log('点击确认回调')
        } else {   
          console.log('点击取消回调')
        }
      }
    })
  },
  popLoading: function(){
    wx.showLoading({
      title:'加载中...'
    })
//隐藏loading提示框
    setTimeout(function(){
    wx.hideLoading()
    },2000)
  },
  popSelect: function(){
    wx.showActionSheet({
      itemList: ['A', 'B', 'C'],
      success: function (res) {
        console.log(res);
      }
    })
  }
});

 

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值