微信小程序底部彈框-組件封裝

底部彈框,遮罩層淡入淡出,彈框高度根據(jù)內(nèi)容自適應(yīng)。
直接上代碼

<!-- modal.wxml -->
<view class="wrap" hidden="{{!myVisible}}">
  <view class="mask {{visible ? 'mask-show' : ''}}" bindtap="_onCancel"></view>
  <view class="box" id="modal-box" animation="{{animationData}}">
    <slot />
  </view>
</view>
//modal.js
Component({
  properties: {
    myVisible: {
      type: Boolean,
      value: false,
      observer: '_visibleChange',
    },
  },

  data: {
    visible: false,
    animation: null,
    animationData: null,
  },

  ready: function () {
    const animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0,
    });
    this.setData({
      animation,
    })
  },

  methods: {
    _visibleChange: function (newVal, oldVal, changedPath) {
      if (oldVal === false && newVal === true) {
        setTimeout(function () {
          this._onShow();
        }.bind(this), 0)
      }
    },

    _onShow: function () {
      const __this = this;
      const query = wx.createSelectorQuery().in(this);
      query.select('#modal-box').boundingClientRect(function (res) {
        const { animation } = __this.data;
        animation.translateY(-res.height).step();
        __this.setData({
          visible: true,
          animationData: animation.export(),
        })
      }).exec();
    },
    
    _onCancel: function () {
      const { animation } = this.data;
      animation.translateY(0).step();
      this.setData({
        visible: false,
        animationData: animation.export(),
      })
      setTimeout(function () {
        this.triggerEvent('myOnCancel');
      }.bind(this), 200)
    },

  },
})
/* modal.wxss */
.wrap {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 99999;
  width: 100vw;
  height: 100vh;
}

.mask {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
  background: #000;
  opacity: 0;
  transition: 0.2s;
}
.mask-show {
  opacity: 0.4;
}

.box {
  position: fixed;
  top: 100vh;
  left: 0;
  z-index: 2;
  width: 100%;
  min-height: 100rpx;
  background: #fff;
}
//modal.json(這句話需要?jiǎng)h了,這里僅為注釋)
{
  "component": true,
  "usingComponents": {}
}

測(cè)試用例

<button
  bindtap="handleShow"
>
  test modal show
</button>
<modal
  myVisible="{{visible}}"
  bindmyOnCancel="handleCancle"
>
  123
</modal>
Page({
  data: {
    visible: false,
  },

  handleShow: function () {
    this.setData({ visible: true });
  },

  handleCancel: function () {
    this.setData({ visible: false });
  },
})
{
  "usingComponents": {
    "modal": "/components/modal/modal"
  },
  "navigationBarTitleText": "demo"
}

whosMeya

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 黑盒測(cè)試案例設(shè)計(jì)技術(shù)篇 1 概述 本章介紹黑盒測(cè)試的概念和進(jìn)行黑盒測(cè)試的目的與意義,及關(guān)于等價(jià)類劃分、邊界值分析、...
    西邊人閱讀 17,346評(píng)論 0 41
  • 文章來(lái)自:http://blog.csdn.net/mj813/article/details/52451355 ...
    好大一只鵬閱讀 9,371評(píng)論 2 126
  • 1.測(cè)試與軟件模型 軟件開(kāi)發(fā)生命周期模型指的是軟件開(kāi)發(fā)全過(guò)程、活動(dòng)和任務(wù)的結(jié)構(gòu)性框架。軟件項(xiàng)目的開(kāi)發(fā)包括:需求、設(shè)...
    Mr希靈閱讀 22,409評(píng)論 7 278
  • 1.測(cè)試與軟件模型 軟件開(kāi)發(fā)生命周期模型指的是軟件開(kāi)發(fā)全過(guò)程、活動(dòng)和任務(wù)的結(jié)構(gòu)性框架。軟件項(xiàng)目的開(kāi)發(fā)包括:需求、設(shè)...
    宇文臭臭閱讀 6,877評(píng)論 5 101
  • 1****、問(wèn):你在測(cè)試中發(fā)現(xiàn)了一個(gè)bug****,但是開(kāi)發(fā)經(jīng)理認(rèn)為這不是一個(gè)bug****,你應(yīng)該怎樣解決? 首...
    蛋炒飯_By閱讀 5,399評(píng)論 1 94

友情鏈接更多精彩內(nèi)容