欢迎访问宙启技术站
智能推送

微信小程序如何实现自定义纯净模态框

发布时间:2023-05-15 22:53:28

微信小程序作为目前最为流行的应用程序,早已成为人们生活不可或缺的一部分。而在小程序开发中,模态框是常用组件之一,尤其是在实现用户操作确认的时候。然而,如果使用官方提供的模态框样式,会显得比较呆板,不够精致。所以,自定义纯净模态框,已成为小程序开发过程中必不可少的技术提升。

本文就通过下面的方式,为大家介绍如何实现自定义纯净模态框,让小程序变得更加生动和具有互动性。

一、设计模态框

在设计模态框之前,需要预先考虑以下问题:

1、模态框需要放置的位置(居中或顶部,底部等)。

2、模态框需要显示哪些内容(标题,提示信息和确认按钮)。

3、模态框的大小和样式(颜色,样式等)。

在设计完成模态框之后,需要将样式CSS文件中进行渲染,可以使用css3或者sass等流行的样式语言进行编辑,这些语言比较容易上手,不需要太多的学习成本。

样式大致如下:

.modal    {

  width: 300px;

  height: 200px;

  margin:auto;

  box-shadow: 0px 0px 10px 2px rgba(0,0,0,.2);

  background-color: white;

  position: relative;

}

 

.modal-header   {

  background-color: #f2f2f2;

  height: 40px;

  line-height: 40px;

  padding-left: 20px;

  font-size: 16px;

  font-weight: bold;

  position: absolute;

  top: 0;

  left: 0;

  right: 0;

  z-index: 1;

}

 

.modal-body {

  margin-top: 40px;

  padding: 20px;

  overflow: auto;

  height: 100px;

}

 

.modal-footer  {

  width: 100%;

  height: 50px;

  line-height: 50px;

  position:absolute;

  bottom:0;

  left:0;

  background-color: #f2f2f2;

  border-top: 1px solid #e4e4e4;

  text-align: center;

}

 

.confirm-btn, .cancel-btn {

  display: inline-block;

  width: 40%;

  height: 35px;

  line-height: 35px;

  margin: 10px;

  border-radius: 6px;

  color: #fff;

  font-size: 14px;

  cursor: pointer;

}

 

.confirm-btn {

  background-color: #8ec1d5;

}

 

.cancel-btn{

  background-color: #e4e4e4;

}

二、实现模态框

在小程序中实现模态框,主要是利用wxml,js和css这三个技术知识点。

具体实现步骤如下:

1、首先创建一个新的组件,文件名为ModalView.wxml,用于存放自定义的模态框内容。

2、在ModalView.wxml文件中,将模态框内容进行HTML代码的编写。

例如:

<!-- 模态框    -->

 

<view class="modal">

  <view class="modal-header">{{title}}</view>

  <view class="modal-body">

    <slot name="modalContent">我是默认内容</slot>

  </view>

  <view class="modal-footer">

    <view class="confirm-btn" bindtap="confirmTap">{{confirmText}}</view>

    <view class="cancel-btn" bindtap="cancelTap">{{cancelText}}</view>

  </view>

</view>

在实现步骤中“slot”标签用于替代所有在这里注入的内容。在这里,一个default slot表示没有指定slot时使用的内容。

3、在ModalView.js文件中,将组件作为父/子组件调用,同时实现相关事件,对组件内容进行处理。

例子:

Component({

  properties: {

    title: {

      type: String,

      value: '标题'

    },

    confirmText: {

      type: String,

      value: '确定'

    },

    cancelText: {

      type: String,

      value: '取消'

    },

    isShow: {

      type: Boolean,

      value: false

    },

    position: {

      type: String,

      value: 'bottom'

    }

  },

 

  methods: {

    confirmTap: function () {

      this.triggerEvent('confirm', {}, {});

    },

 

    cancelTap: function () {

      this.triggerEvent('cancel', {}, {});

    }

  },

 

  observers: {

    'isShow': function (value) {

      var animation = wx.createAnimation({

        duration: 300,

        timingFunction: 'linear',

        delay: 0

      })

 

      animation.opacity(value ? 1 : 0).step()

 

      this.setData({

        modalAnimation: animation.export()

      });

    }

  }

})

在监听的过程中,通过wx.createAnimation()方法,定义了模态框的动画效果。

4、使用模态框组件

假设ModalView组件放置在pages/test/test.wxml文件假设,那么在实际使用时,只需要在test.js文件中的代码来调用该组件,实现完整的模态框。

例子:

<modal-view title='提醒' confirmText='确定' cancelText='取消'

                isShow='{{isShow}}' bind:cancel='handleCancel' bind:confirm='handleConfirm'>

    <text>这是模态框自定义的内容</text>

</modal-view>

在具体使用的过程中,通过标记“<modal-view>”标签来引用组件,同时将模态框的样式,标题和内容等信息都设置给该组件,按照实际情况来制定逻辑代码,实现模态框内容的处理。

总结

本文通过具体的实现代码,为大家详细阐述了如何实现自定义纯净模态框,相信读者在学习后,能够轻松构建自己的小程序模态框,并加深对小程序开发的理解。