小程序自定义showModal弹框

纯属练习组件化

在这里插入图片描述
组件wxml页面代码如下

<view class='shade' hidden='{{popup}}' ></view>
<view class='shade_box popup {{frameAnimate}}' hidden='{{popup}}'>
    <view class='content'>{{ title }}</view>
     <!-- 内容 -->
     <view style='min-height:100rpx;'> {{ content }}</view>
   <view class='bottom-btn'>
   <view   bindtap='_hideEvent' wx:if='{{ cancelBtn }}'>{{ cancelBtn }}</view> //这一块是因为有时候弹框可能只需要一个按钮,
    <view  bindtap='_confirmEvent'>{{ confirmBtn }}</view>
   </view>
</view>

wxss页面代码如下

/* pages/middleAnimation/middleAnimation.wxss */

/* 遮罩 */

.shade {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: rgba(0, 0, 0, 0.3);
  z-index: 10;
}

.shade_box {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  margin: auto;
  z-index: 11;
  min-width: 200rpx;
  min-height: 200rpx;
  font-size: 28rpx;
  box-sizing: border-box;
  color: #333;
  font-family: Helvetica, "Helvetica Neue", Arial, sans-serif;
  letter-spacing: 0;
  word-wrap: break-word;
}

.frameAnimate {
  animation-name: popup;
  animation-duration: 0.2s;
  animation-timing-function: linear;
  animation-delay: 0;
  animation-iteration-count: 1;
  animation-direction: normal;
}

@keyframes popup {
  from {
    opacity: 0;
    transform: scale(0.3, 0.3);
  }

  to {
    opacity: 1;
    transform: scale(1, 1);
  }
}

/* 当前弹窗样式 */

.popup {
  width: 600rpx;
  height: 314rpx;
  background-color: #fff;
  border-radius: 30rpx;
  padding: 0 40rpx;
  padding-top: 60rpx;
  box-sizing: border-box;
}

.popup .content {
  font-family: PingFangSC-Semibold, PingFang SC;
  font-weight: 600;
  font-size: 40rpx;
  color: #11141A
}
.bottom-btn{
  display: flex;
  align-items: center;
  border-top: 1rpx solid #ececec;
  height: 100rpx;
  text-align: center
}
.bottom-btn view{
 flex: 1
}

逻辑层代码如下

// component/frameAnimation/frameAnimation.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    content:{
      type:String,
      value:''
    },
    title: {
      type: String,
      value: '我是一个动画的特效动画'
    },
    cancelBtn: {
      type: String,
      value: ''
    },
    confirmBtn: {
      type: String,
      value: '确认'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    popup:true,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    showFrame:function(){
      this.setData({
        popup: false,
        frameAnimate: 'frameAnimate'
      })
    },
    hideFrame() {
      this.setData({
        popup: true,
        frameAnimate: ''
      })
    },
    _confirmEvent() {
      this.triggerEvent("confirmEvent");
    },
    _hideEvent() {
      this.triggerEvent("hideEvent"); //将事件传递到调用组件的页面去操作
    }
  }
})

在使用页面 引入组件 “frameAnimation”: “/component/frameAnimation/frameAnimation”,

<view  style='margin:20rpx 50rpx;'>
    <button type='primary' bindtap='showPopup2'>打开弹窗</button>
</view>

<view  style='margin:20rpx 50rpx;'>
    <button type='primary' bindtap='showPopup3'>打开弹窗2</button>
</view>

  <frameAnimation id='frameAnimation' 
   content=''
   title='动画'
   cancelBtn='取消'
   bind:confirmEvent='_confirmEvent'
   bind:hideEvent='_hideEvent'
   >   
  
  </frameAnimation>


  <frameAnimation id='frameAnimation1' 
   content='拨打司机电话,联系司机'
   title='动画'
   bind:confirmEvent='_confirmEvent1'
   bind:hideEvent='_hideEvent1'
   >   
  
  </frameAnimation>

逻辑层

  // 以下部分是有动画的弹框方法
  showPopup2:function(){
    this.selectComponent("#frameAnimation").showFrame();
  },
  // 以下部分是有动画的弹框方法
  showPopup3: function () {
    this.selectComponent("#frameAnimation1").showFrame();
  },
  // 公用弹框确认方法
  _confirmEvent:function(){
    console.log('点击了确认按钮')
    this.selectComponent("#frameAnimation").hideFrame();
  },
  _hideEvent:function(){
    this.selectComponent("#frameAnimation").hideFrame();
  },
  _confirmEvent1: function () {
    console.log('点击了确认按钮')
    this.selectComponent("#frameAnimation1").hideFrame();
  },
    _hideEvent1: function () {
    this.selectComponent("#frameAnimation1").hideFrame();
  },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值