微信小程序之自定義模態框



爲什麼要自定義模態框?

因爲官方提供的模態框缺乏靈活性,無法滿足每個人的需求,所以我們可以根據自己的實際需求來實現自定義模態框。

效果圖

在這裏插入圖片描述

實現思路

通過一個蒙層+自定義的彈窗來實現上圖的效果,而彈窗的消失與出現則通過一個狀態值來控制即可。

界面代碼

index.wxml

<!-- 請在需要觸發彈窗的地方添加以下函數,可以是按鈕或者文本框 -->
bindtap="showDialogBtn"

<!-- 自定義彈窗 -->
<view class="modal-mask" bindtap="hideModal" catchtouchmove="true" wx:if="{{showModal}}"></view>
<view class="modal-dialog" wx:if="{{showModal}}">
    <view class="modal-title">修改求職意向</view>
    <view class="modal-content">
        <view class="modal-input">
            <input placeholder-class="input-holder" type="number" maxlength="10" bindinput="inputChange" class="input" placeholder="請輸入您的求職意向"></input>
        </view>
    </view>
    <view class="modal-footer">
        <view class="btn-cancel" bindtap="onCancel" data-status="cancel">取消</view>
        <view class="btn-confirm" bindtap="onConfirm" data-status="confirm">確定</view>
    </view>
</view>

樣式代碼

index.wxss

/* 自定義彈窗樣式 */
.modal-mask {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: #000;
  opacity: 0.5;
  overflow: hidden;
  z-index: 9000;
  color: #fff;
}

.modal-dialog {
  width: 540rpx;
  overflow: hidden;
  position: fixed;
  top: 50%;
  left: 0;
  z-index: 9999;
  background: #f9f9f9;
  margin: -180rpx 105rpx;
  border-radius: 36rpx;
}

.modal-title {
  padding-top: 50rpx;
  font-size: 45rpx;
  color: #030303;
  text-align: center;
}

.modal-content {
  padding: 50rpx 32rpx;
}

.modal-input {
  display: flex;
  background: #fff;
  border: 2rpx solid #ddd;
  border-radius: 4rpx;
  font-size: 35rpx;
}

.input {
  width: 100%;
  height: 82rpx;
  font-size: 35rpx;
  line-height: 35rpx;
  padding: 0 20rpx;
  box-sizing: border-box;
  color: #333;
}

input-holder {
  color: #666;
  font-size: 35rpx;
}

.modal-footer {
  display: flex;
  flex-direction: row;
  height: 86rpx;
  border-top: 1px solid #dedede;
  font-size: 40rpx;
  line-height: 86rpx;
}

.btn-cancel {
  width: 50%;
  color: #666;
  text-align: center;
  border-right: 1px solid #dedede;
}

.btn-confirm {
  width: 50%;
  color: #ec0033;
  text-align: center;
}
/* 自定義彈窗樣式 */

邏輯代碼

index.js

Page({
    /**
     * 頁面的初始數據
     */
    data: {
        showModal: false,
        inputValue: null
    },
    /**
     * 彈窗
     */
    showDialogBtn: function() {
        this.setData({
            showModal: true
        })
    },
    /**
     * 彈出框蒙層截斷touchmove事件
     */
    preventTouchMove: function() {
        return
    },
    /**
     * 隱藏模態對話框
     */
    hideModal: function() {
        this.setData({
            showModal: false
        });
    },
    /**
     * 對話框取消按鈕點擊事件
     */
    onCancel: function() {
        this.hideModal();
    },
    /**
     * 對話框確認按鈕點擊事件
     */
    onConfirm: function() {
        this.hideModal();
    },

    /**
     * 獲取輸入框中輸入的值
     */
    inputChange:function(option){
        /* 把文本框輸入的內容方到 data 裏面 */
        this.setData({
            inputValue: option.detail.value
        })
    }
})

解讀

    preventTouchMove: function() {
        return
    }
結合界面代碼(index.wxml)來看,蒙層 view 裏有一個事件綁定 catchtouchmove="preventTouchMove"。這樣寫的原因是阻斷事件向下傳遞,避免在彈窗後還可以點擊或者滑動蒙層下的界面。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章