微信小程序封装自定义弹窗

最近在做小程序的登录,需要同时获取用户手机号和头像昵称等信息,但是小程序又不支持单个接口同时获取两种数据,因此想到自定义一个弹窗,通过弹窗按钮触发获取手机号事件。记录一下。

具体代码如下:

业务代码中:

  在业务代码中引入dialog组件即可

  <dialog visible="{{dialogVisible}}" showFooter="{{footerVisible}}" title="测试一下">

    <view class=‘dialog-body‘ slot="dialog-body">

      <view class=‘dialog-content‘>申请获取你微信绑定的手机号</view>

      <view class=‘dialog-footer‘ slot="dialog-footer">

        <button class=‘cancel-btn‘ bindtap="close">取消</button>

        <button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber" class=‘confirm-btn‘>授权</button>

      </view>

    </view>

  </dialog>

dialog组件:

component下面新建dialog。注意是 component 不是 page ,因为要作为组件引入到页面中

  dialog.wxml:

  需要传入四个属性

    visible:是否显示弹窗

    title :标题

    showClose:是否显示右上角关闭按钮

    showFooter:是否显示底部按钮

<!--components/dialog/dialog.wxml-->

<view class=‘dialog-custom‘ wx:if="{{visible}}">

  <view class=‘dialog-mask‘ bindtap="clickMask"></view>

    <view class="dialog-main">

      <view class="dialog-container">

        <view class=‘dialog-container__title‘ wx:if="{{title.length>0}}">

          <view class=‘title-label‘>{{ title }}</view>

          <view class=‘title-icon‘>

            <image wx:if="{{showClose}}" bindtap=‘close‘ src=‘/images/close-btn.png‘></image>

          </view>

        </view>

      <view class=‘dialog-container__body‘>

        <slot name="dialog-body"></slot>

      </view>

      <view class=‘dialog-container__footer‘ wx:if="{{showFooter}}">

        <view class=‘dialog-container__footer__cancel‘ bindtap="close">取消</view>

        <view class=‘dialog-container__footer__confirm‘ bindtap=‘confirm‘>确定</view>

      </view>

    </view>

  </view>

</view>

dialog.js

  

Component({

/**

* 组件的属性列表

*/

properties: {

  visible: {

    type: Boolean,

    value: false

  },

  width: {

    type: Number,

    value: 85

  },

  position: {

    type: String,

    value: ‘center‘

  },

  title: {

    type: String,

    value: ‘‘

  },

  showClose: {

    type: Boolean,

    value: true

  },

  showFooter: {

    type: Boolean,

    value: false

  },

},

/**

* 组件的初始数据

*/

data: {

},

options:{

  multipleSlots: true

},

/**

* 组件的方法列表

*/

methods: {

  clickMask() {

    this.setData({ visible: false });

  },

  close(){

    this.setData({ visible: false });

  },

  cancel() {

    this.setData({ visible: false });

    this.triggerEvent(‘cancel‘);

  },

  confirm() {

    this.setData({ visible: false });

    this.triggerEvent(‘confirm‘);

  }

}

})

dialog.json:声明是组件就行

{

  "component": true,

  "usingComponents": {}

}

dialog.wxss

  css可以根据自己喜好的样式调整,注意mask遮罩层的z-index高一点,确保在最上层

/* components/dialog/dialog.wxss */

.dialog-custom {

  width: 100vw;

  height: 100%;

  position: absolute;

  left: 0;

  top: 0;

  z-index: 9999;

}

.dialog-mask {

  position: fixed;

  top: 0;

  left: 0;

  right: 0;

  bottom: 0;

  z-index: 10000;

  width: 100vw;

  height: 100%;

  background: rgba(0, 0, 0, 0.3);

}

.dialog-main {

  position: fixed;

  z-index: 10001;

  top: 50%;

  left: 0;

  right: 0;

  width: 85vw;

  height: auto;

  margin: auto;

  transform: translateY(-50%);

}

.dialog-container {

  margin: 0 auto;

  background: #fff;

  z-index: 10001;

  border-radius: 3px;

  box-sizing: border-box;

  padding: 40rpx;

}

.dialog-container__title {

  width: 100%;

  height: 50rpx;

  line-height: 50rpx;

  margin-bottom: 20rpx;

  position: relative;

}

.dialog-container__title .title-label{

  display: inline-block;

  width: 100%;

  height: 50rpx;

  line-height: 50rpx;

  font-size: 36rpx;

  color: #000;

  text-align: center;

}

.dialog-container__title .title-icon{

  width: 34rpx;

  height: 50rpx;

  position: absolute;

  top: 0;

  right: 0;

}

.dialog-container__title .title-icon image{

  width: 34rpx;

  height: 34rpx;

}

.dialog-container__body {

  padding-top: 10rpx;

  font-size: 32rpx;

  line-height: 50rpx;

}

.dialog-container__footer {

  height: 76rpx;

  line-height: 76rpx;

  font-size: 32rpx;

  text-align: center;

  border-top: 1px solid #f1f1f1;

  position: absolute;

  bottom: 0;

  left: 0;

  right: 0;

}

.dialog-container__footer .dialog-container__footer__cancel {

  width: 50%;

  color: #999;

  display: inline-block;

}

.dialog-container__footer .dialog-container__footer__cancel::after{

  position: absolute;

  right: 50%;

  bottom: 0;

  content: ‘‘;

  width: 2rpx;

  height: 76rpx;

  background: #f1f1f1;

}

.dialog-container__footer .dialog-container__footer__confirm {

  color: #3B98F7;

  width: 50%;

  display: inline-block;

  text-align: center;

}

/* components/dialog/dialog.wxss */

.dialog-custom {

width: 100vw;

height: 100%;

position: absolute;

left: 0;

top: 0;

z-index: 9999;

}

.dialog-mask {

position: fixed;

top: 0;

left: 0;

right: 0;

bottom: 0;

z-index: 10000;

width: 100vw;

height: 100%;

background: rgba(0, 0, 0, 0.3);

}

.dialog-main {

position: fixed;

z-index: 10001;

top: 50%;

left: 0;

right: 0;

width: 85vw;

height: auto;

margin: auto;

transform: translateY(-50%);

}

.dialog-container {

margin: 0 auto;

background: #fff;

z-index: 10001;

border-radius: 3px;

box-sizing: border-box;

padding: 40rpx;

}

.dialog-container__title {

width: 100%;

height: 50rpx;

line-height: 50rpx;

margin-bottom: 20rpx;

position: relative;

}

.dialog-container__title .title-label{

display: inline-block;

width: 100%;

height: 50rpx;

line-height: 50rpx;

font-size: 36rpx;

color: #000;

text-align: center;

}

.dialog-container__title .title-icon{

width: 34rpx;

height: 50rpx;

position: absolute;

top: 0;

right: 0;

}

.dialog-container__title .title-icon image{

width: 34rpx;

height: 34rpx;

}

.dialog-container__body {

padding-top: 10rpx;

font-size: 32rpx;

line-height: 50rpx;

}

.dialog-container__footer {

height: 76rpx;

line-height: 76rpx;

font-size: 32rpx;

text-align: center;

border-top: 1px solid #f1f1f1;

position: absolute;

bottom: 0;

left: 0;

right: 0;

}

.dialog-container__footer .dialog-container__footer__cancel {

width: 50%;

color: #999;

display: inline-block;

}

.dialog-container__footer .dialog-container__footer__cancel::after{

position: absolute;

right: 50%;

bottom: 0;

content: ‘‘;

width: 2rpx;

height: 76rpx;

background: #f1f1f1;

}

.dialog-container__footer .dialog-container__footer__confirm {

color: #3B98F7;

width: 50%;

display: inline-block;

text-align: center;

}

原文地址:https://www.cnblogs.com/Indus/p/10826418.html

时间: 2024-08-01 15:08:38

微信小程序封装自定义弹窗的相关文章

微信小程序之自定义toast弹窗

微信小程序里面的自带弹窗icon只有两种,success和loading.有时候用户输入错误的时候想加入一个提醒图标,也可以使用wx.showToast中的image来添加图片达到使用自定义图标的目的:但是如果图标是字体,或者提醒的内容有很长捏(小程序中提醒的内容最多只能设置7个字,多了会被隐藏),那就只有自定义toast弹窗了: 第一步:新建一个wxml文件用来装模板,方便以后使用,比如 然后在这里面添加模板代码 <template name="toast"> //nam

微信小程序之自定义tabbar

0. 官方指南 # 由于微信小程序的异步机制,页面跳转会出现随机事件 # 目的: 解决认证跳转过程的不友好体验 https://developers.weixin.qq.com/miniprogram/dev/framework/ability/custom-tabbar.html 1. app.json文件的处理 1. 添加:"custom": true,使用自定义的tabbar 2. 将非tabbar的内容删除,删除的示例是发布,如下: "tabBar": {

如何使用微信小程序开发一个弹窗页面(附源码)

在小程序的开发过程中,我们肯定会遇到开发一个弹窗页面的情况,我们先看一下小程序官方对于弹窗页面的解释.API的接口如下 从官方给出的代码示例来看,想当简单,就像一个asert,并不能看出弹窗的真实需求.所以今天HTML51.COM就写了一个弹窗小程序教程,供大家学习参考.首页我们先看一下动态的效果图: 我们首先看到的是首页代码: <view class="copyright"> <view class="copyright_item">Cop

微信小程序封装请求的js

1.配置访问服务器的地址config.js: const config = {//192.18.1.2:8083 https://www.so.com/ api_base_url: 'http://192.168.1.12:8083', // api_base_url:"https://www.baidu.com", img_base_url: '' } export { config } 2.封装http请求http.js: import { config } from './con

微信小程序封装bindinput &amp; 输入框出现清空图标 &amp; wx:key对input的影响

Q:我以前写小程序每次获取输入内容,都要写一个方法,觉得十分麻烦,所以写了一个通用的方法. A:我能想到的原理就是,不同的input所带的data不同,bindinput事件setData不同的data. <input class="weui-input" data-inputName='name' placeholder="你的姓名" bindinput="bindKeyInput" value='{{item}}' bindfocus=

微信小程序覆盖自定义组件样式

小程序官方文档明确指出,引入的第三方自定义组件,是不可以对其进行CSS样式覆盖的,但是我们还想要修改怎么办呢?自定义组件时会之定义个外部类,通过这个外部类来修改样式. 修改https://weapp.iviewui.com/所提供的组件样式为例,它定义了一个外部类接口: 1. 通过第三方组件给出的externalClasses: ['i-class'],来指定自己的样式类 自己的wxml i-class="myrate" <i-rate i-class="myrate&

微信小程序之自定义模态弹窗(带动画)实例

1.基本需求. 实现用户自定义弹框 带动画(动画可做参靠,个人要是觉得不好看可以自定义动画) 获取弹出框的内容,自定义事件获取 2.案例目录结构 二.程序实现具体步骤 1.弹框index.wxml代码 <!--button--> <view class="btn" bindtap="powerDrawer" data-statu="open">来点我呀</view> <!--mask--> <

微信小程序安卓固定弹窗中textarea的placeholder会被弹出去

在项目中碰到一个问题,就是安卓机上,如果fix固定一个弹窗,这个弹窗中有textarea的话, 点击弹出弹窗,textarea获得焦点的时候,placeholder和value会被弹出到背景上.如图: 弹窗wxml代码: 1 <view class="common-modal" wx:if="{{!actionSheetHidden}}"> 2 <view class="app_container "> 3 4 <v

微信小程序封装http请求

Q:网上很多封装http请求的方法,我就收藏了一种比较通俗易懂的方法. A:对封装的要求:一是在封装函数里设置header,发送token.二是在封装函数里对后台返回的参数作出统一处理,例如如果statusCode不返回200,统一做出异常处理,相当于过滤功能. var API_COMMON = 'http://192.168.2.105:8080/' var requestHandler = { params: {}, success: function (res) { // success