微信小程序四种弹窗方式实例

微信小程序四种弹窗方式实例

一、wx.showToast(Object object)

显示消息提示框

  wx.showToast({
     title: '内容', //提示的内容
     duration: 2000, //持续的时间
     icon: 'loading', //图标有success、error、loading、none四种
     mask: true //显示透明蒙层 防止触摸穿透
  })

二 、wx.showModal(Object object)

显示模态对话框

wx.showModal({
     title: '我是标题', //提示的标题
     content: '我是内容', //提示的内容
     success: function(res) {
       if(res.confirm) {
         console.log('用户点击了确定')
       } else if (res.cancel) {
         console.log('用户点击了取消')
       }
     }
   })

三、wx.showLoading(Object object)

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

 wx.showLoading({
   title: '加载中',
 })
 ​
 setTimeout(function () {
   wx.hideLoading()
 }, 2000)

|注意:

wx.showLoading 和 wx.showToast 同时只能显示一个wx.showLoading 应与 wx.hideLoading 配对使用

四、wx.showActionSheet(Object object)

显示操作菜单

 wx.showActionSheet({
    itemList:['选项一','选项二','其它'], //文字数组
    success: (res) => {
      switch(res.tapIndex) {
        case 0:
          console.log('点击了 选项一')
          break;
        case 1:
          console.log('点击了 选项二')
          break; 
        case 2:
          console.log('点击了 其它选项')
          break;
      }
    },
    fail (res) {
      console.log('取消选项')
    }
  })

五、官方文档

具体参数可见官网:微信小程序弹窗

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是微信小程序开发实现弹窗签到的步骤和代码示例: 1. 在wxml文件中添加签到按钮和弹窗组件 ```html <!-- 签到按钮 --> <button bindtap="showModal">签到</button> <!-- 弹窗组件 --> <view class="modal" hidden="{{!modalHidden}}"> <view class="modal-dialog"> <view class="modal-header"> <text class="modal-title">签到成功</text> </view> <view class="modal-body"> <text>恭喜您获得了一次抽奖机会!</text> </view> <view class="modal-footer"> <button class="modal-button" bindtap="hideModal">确定</button> </view> </view> </view> ``` 2. 在js文件中添加弹窗显示和隐藏的函数 ```javascript Page({ data: { modalHidden: true }, showModal: function() { this.setData({ modalHidden: false }) }, hideModal: function() { this.setData({ modalHidden: true }) } }) ``` 3. 在wxss文件中添加弹窗的样式 ```css .modal { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); z-index: 999; } .modal-dialog { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 80%; max-width: 400rpx; background-color: #fff; border-radius: 10rpx; overflow: hidden; } .modal-header { padding: 20rpx; text-align: center; background-color: #f8f8f8; } .modal-title { font-size: 32rpx; font-weight: bold; } .modal-body { padding: 20rpx; text-align: center; } .modal-footer { padding: 20rpx; text-align: center; background-color: #f8f8f8; } .modal-button { display: inline-block; padding: 10rpx 20rpx; border: 1rpx solid #ccc; border-radius: 5rpx; background-color: #fff; color: #333; } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值