微信小程序自定义模态框

博客介绍了模态框相关内容,包含模态框内容的自定义,以及页面调用模态框组件的代码,涉及 HTML、JS 语言。同时还给出了组件代码,涵盖 HTML、CSS 和 JS 部分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述
模态框内容部分自定义内容

页面调用模态框组件代码

HTML

<popup bind:pConfirm='pConfirm' show='{{show}}'>
    <view style="display: flex;justify-content: center;margin-top:40rpx">自定义内容</view>
</popup>

JS

Page({
  data: {
    show:'none'
  },
//控制模态框显示
  showPopup(){
    this.setData({
      show:"block"
    })
  },
  //模态框确定方法
  pConfirm(){
    console.log('外部方法')
  },
  
  onLoad() {
    
  },
})

组件代码
HTML

<view class="p-mask" style="display:{{updateShow}}" catchtouchmove="{{true}}"></view>
<view class="p-update" style="display:{{updateShow}};transform:{{scale}};height:{{height}}rpx;width:{{width}}rpx" catchtouchmove="{{true}}">
    <slot></slot>
    <view class="p-wrap">
        <view class="p-btn-1" bindtap='close'>取消</view>
        <view class="p-btn-2" bindtap='confrim'>确定</view>
    </view>
</view>

CSS

.p-mask {
    background-color: black;
    opacity: .3;
    width: 100vw;
    height: 100vh;
    position: fixed;
    top: 0;
    z-index: 100;
}
.p-update  {
    background-color: white;
    position: fixed;
    z-index: 101;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
    width: 614rpx;
    height: 436rpx;
    border-radius: 26rpx;
    font-size: 32rpx;
    box-sizing: border-box;
    box-shadow: 0 0 30rpx 2rpx rgb(151, 149, 149);
    transition: .2s ease all;
    overflow: hidden;
}
.p-wrap {
    width: 100%;
    height: 106rpx;
    position: absolute;
    bottom: 0;
    border-top: 1rpx solid #EBEDF5;
    box-sizing: border-box;
    display: flex;
}
.p-btn-1 {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    width: 50%;
    border-right: 1rpx solid #EBEDF5;
}
.p-btn-2 {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    width: 50%;
}

JS

Component({
  // 启用插槽
  options: {
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: String,
      value: 'none'
    },
    //弹框高度
    height: {
      type: String,
      value: '436'
    },
    //宽度
    width:{
      type:String,
      value:'614'
    }
  },
  observers: {
    'show'(data) {
      if (data == 'none'){
        this.setData({
          updateShow:data
        },_=>{
          return
        })
      }
      this.setData({
        updateShow: data
      }, _ => {
        this.setData({
          scale: 'scale(1)'
        })
      })
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    scale: 'scale(0)',
    updateShow: 'none'
  },

  /**
   * 组件的方法列表
   */
  methods: {
    confrim(){
      this.triggerEvent('pConfirm')
    },
    close() {
      this.setData({
        scale: 'scale(0)'
      }, _ => {
        this.setData({
          updateShow: 'none'
        })
      })
    },
  }
})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值