小程序 弹窗组件_小程序怎么开发

小程序 弹窗组件_小程序怎么开发本文主要分成了两个部分,第一部分为微信小程序弹窗组件的具体步骤,第二部分为小程序的多种弹窗类型

本文主要分成了两个部分,第一部分为微信小程序弹窗组件的具体步骤,第二部分为小程序的多种弹窗类型。

第一部分 微信小程序弹窗组件的具体步骤

1.新建component文件夹存放我们的组件,里边存放的就是我们所用的组件,我们今天要做的事弹出框,新建文件夹popup存放我们的组件模板,点击右键选择新建component,就会自动生成组件的模板wxss、wxml、json、js,如图

在这里插入图片描述

2.组件布局和样式 :
popup.wxml
<view class="wx-popup" hidden="{ 
    {flag}}">
  <view class='popup-container'>
    <view class="wx-popup-title">{
  
  {title}}</view>
    <view class="wx-popup-con">{
  
  {content}}</view>
    <view class="wx-popup-btn">
      <text class="btn-no" bindtap='_error'>{
  
  {btn_no}}</text>
      <text class="btn-ok" bindtap='_success'>{
  
  {btn_ok}}</text>
    </view>
  </view>
</view>
popup.wxss
.wx-popup { 
   
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, .5);
}
.popup-container { 
   
  position: absolute;
  left: 50%;
  top: 50%;
 
  width: 80%;
  max-width: 600rpx;
  border: 2rpx solid #ccc;
  border-radius: 10rpx;
  box-sizing: bordre-box;
  transform: translate(-50%, -50%); 
  overflow: hidden;
  background: #fff;
}
.wx-popup-title { 
   
  width: 100%;
  padding: 20rpx;
  text-align: center;
  font-size: 40rpx;
  border-bottom: 2rpx solid red;
}
.wx-popup-con { 
   
  margin: 60rpx 10rpx;
  text-align: center;
}
.wx-popup-btn { 
   
  display: flex;
  justify-content: space-around;
  margin-bottom: 40rpx;
}
.wx-popup-btn text { 
   
  display: flex;
  align-items: center;
  justify-content: center;
  width: 30%;
  height: 88rpx;
  border: 2rpx solid #ccc;
  border-radius: 88rpx;
}
popup.js
Component({ 
   
  options: { 
   
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  /** * 组件的属性列表 */
  properties: { 
   
    title: { 
               // 属性名
      type: String,     // 类型(必填),目前接受的类型包括:String, Number, Boolean, Object, Array, null(表示任意类型)
      value: '标题'     // 属性初始值(可选),如果未指定则会根据类型选择一个
    },
    // 弹窗内容
    content: { 
   
      type: String,
      value: '内容'
    },
    // 弹窗取消按钮文字
    btn_no: { 
   
      type: String,
      value: '取消'
    },
    // 弹窗确认按钮文字
    btn_ok: { 
   
      type: String,
      value: '确定'
    } 
  },
 
  /** * 组件的初始数据 */
  data: { 
   
    flag: true,
  },
 
  /** * 组件的方法列表 */
  methods: { 
   
    //隐藏弹框
    hidePopup: function () { 
   
      this.setData({ 
   
        flag: !this.data.flag
      })
    },
    //展示弹框
    showPopup () { 
   
      this.setData({ 
   
        flag: !this.data.flag
      })
    },
    /* * 内部私有方法建议以下划线开头 * triggerEvent 用于触发事件 */
    _error () { 
   
      //触发取消回调
      this.triggerEvent("error")
    },
    _success () { 
   
      //触发成功回调
      this.triggerEvent("success");
    }
  }
})
3.在首页用这个组件需要配置一下,首先建一个名为index.json的文件,里边配置”usingComponents”,就是需要引入到首页:
{ 
   
  "usingComponents": { 
   
    "popup": "/component/popup/popup"
  }
}
4.引入到首页
index.wxml
<view class="container">
  <view class="userinfo">
    <button bindtap="showPopup"> 点我 </button>
  </view>
  <popup id='popup' title='小组件' content='学会了吗' btn_no='没有' btn_ok='学会了' bind:error="_error" bind:success="_success">
  </popup>
</view>
5.配置index.js操作点击事件
//获取应用实例
const app = getApp()
 
Page({ 
   
  onReady: function () { 
   
    //获得popup组件
    this.popup = this.selectComponent("#popup");
  },
 
  showPopup() { 
   
    this.popup.showPopup();
  },
 
  //取消事件
  _error() { 
   
    console.log('你点击了取消');
    this.popup.hidePopup();
  },
  //确认事件
  _success() { 
   
    console.log('你点击了确定');
    this.popup.hidePopup();
  }
})

第二部分 小程序的多种弹窗类型

1. 纯文本提示
wx.showToast({ 
   
   title: '纯文字弹窗',
   icon: 'none',    //如果要纯文本,不要icon,将值设为'none'
   duration: 2000     
})  
2. 取消确认提示
 wx.showModal({ 
   
   title: 'confirm的弹窗',
   content: '确认要删除该项吗?',
   success: function (res) { 
   
     if (res.confirm) { 
     
       console.log('点击确认回调')
     } else { 
      
       console.log('点击取消回调')
     }
   }
})
3. 成功提示
wx.showToast({ 
   
   title: '成功提示弹窗',
   icon: '',     //默认值是success,就算没有icon这个值,就算有其他值最终也显示success
   duration: 2000,      //停留时间
})
4. 自定义图标弹窗
wx.showToast({ 
   
   title: '自定义图标弹窗',
   image: '../../static/image/icon.png',  //image的优先级会高于icon
   duration: 2000     
})
5. 加载中提示
wx.showLoading({ 
   
   title:'加载中...'
});
6. 带蒙层的弹窗
wx.showToast({ 
   
    title: '带蒙层的弹窗',     
    duration: 2000,    
    mask:true    //是否有透明蒙层,默认为false 
                 //如果有透明蒙层,弹窗的期间不能点击文档内容 
})
7. 有列表的弹窗
wx.showActionSheet({ 
   
   itemList: ['A', 'B', 'C'],
   success: function (res) { 
   
     console.log(res);
   }
})
隐藏 wx.hideToast() wx.hideLoading()

本文章转载自:
1.小程序的各种弹窗
2.微信小程序弹窗组件详解
如有侵权,请联系,马上删除。

今天的文章小程序 弹窗组件_小程序怎么开发分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/70496.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注