属性介绍

page-container是微信小程序的一个视图容器,主要的功能就是实现一个弹窗的功能,该组件所包含的属性不多,比较关键的属性就是显示容器和容器位置的属性以及各种事件触发函数。如下

 show主要通过一些page-container外的事件触发后更改将show的值改为true,从而显示page-container组件。

position属性控制着组件弹出的位置,但是属性的值bottom,top与right,center的显示效果不同。

bottom和top只是在当前页面中显示出一个弹窗,而right和center是相当于直接载入了一个子页面,

雷区tips

当前页面最多只有 1 个容器,若已存在容器的情况下,无法增加新的容器

自定义实现底部弹出层

wxml

<view class="modals modals-bottom-dialog" hidden="{{hideModal}}">
    <view class="modals-cancel" bindtap="hideModal"></view>
    <view class="bottom-dialog-body bottom-pos" animation="{{animationData}}"></view>
</view>
<button bindtap="showModal">点我</button>

wxss

/*模态框*/
.modals {
    position: fixed;
    z-index: 999;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

.modals-cancel {
    position: absolute;
    z-index: 1000;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, .5);
}

.bottom-dialog-body {
    position: absolute;
    z-index: 10001;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 30rpx;
    height: 85vh;
    background-color: #fff;
    border-radius: 30rpx;
}

/*动画前初始位置*/
.bottom-pos {
    -webkit-transform: translateY(100%);
    transform: translateY(100%);
}

js

  data: {
    hideModal: true, //模态框的状态 true-隐藏 false-显示
    animationData: {},//
  },
  methods: {
    showModal: function () {
      var that = this;
      that.setData({
        hideModal: false
      })
      var animation = wx.createAnimation({
        duration: 300,//动画的持续时间 默认400ms 数值越大,动画越慢 数值越小,动画越快
        timingFunction: 'ease',//动画的效果 默认值是linear
      })
      this.animation = animation
      setTimeout(function () {
        that.fadeIn();//调用显示动画
      }, 200)
    },

    // 隐藏遮罩层
    hideModal: function () {
      var that = this;
      var animation = wx.createAnimation({
        duration: 300,//动画的持续时间 默认400ms 数值越大,动画越慢 数值越小,动画越快
        timingFunction: 'ease',//动画的效果 默认值是linear
      })
      this.animation = animation
      that.fadeDown();//调用隐藏动画 
      setTimeout(function () {
        that.setData({
          hideModal: true
        })
      }, 200)//先执行下滑动画,再隐藏模块

    },

    //动画集
    fadeIn: function () {
      this.animation.translateY(0).step()
      this.setData({
        animationData: this.animation.export()//动画实例的export方法导出动画数据传递给组件的animation属性
      })
    },
    fadeDown: function () {
      this.animation.translateY(650).step()
      this.setData({
        animationData: this.animation.export(),
      })
    },
  }

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐