微信小程序彈框wx.showModal如何修改樣式

由于官方API提供的顯示模態(tài)彈窗,只能簡(jiǎn)單地顯示文字內(nèi)容,不能對(duì)對(duì)話(huà)框內(nèi)容進(jìn)行自定義,欠缺靈活性,所以自己從模態(tài)彈窗的原理角度來(lái)實(shí)現(xiàn)了自定義的模態(tài)對(duì)話(huà)框。

wx.showModal修改樣式后的效果,如下圖所示:

index.wxml代碼:

<!--index.wxml-->
<button class="show-btn" bindtap="showDialogBtn">彈窗</button>
<!--彈窗-->
<view class="modal-mask" bindtap="hideModal" catchtouchmove="preventTouchMove" wx:if="{{showModal}}"></view>
<view class="modal-dialog" wx:if="{{showModal}}">
  <view class="modal-title">添加數(shù)量</view>
  <view class="modal-content">
    <view class="modal-input">
      <input placeholder-class="input-holder" type="number" maxlength="10" bindinput="inputChange" class="input" placeholder="請(qǐng)輸入數(shù)量"></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樣式代碼:

/**index.wxss**/
.show-btn {
  margin-top: 100rpx;
  color: #22cc22;
}
.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: 36rpx;
  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: 28rpx;
}

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

.modal-footer {
  display: flex;
  flex-direction: row;
  height: 86rpx;
  border-top: 1px solid #dedede;
  font-size: 34rpx;
  line-height: 86rpx;
}
.btn-cancel {
  width: 50%;
  color: #666;
  text-align: center;
  border-right: 1px solid #dedede;
}
.btn-confirm {
  width: 50%;
  color: #ec5300;
  text-align: center;
}

index.js代碼如下:

/index.js

//獲取應(yīng)用實(shí)例

var app = getApp()
Page({
  data: {
    showModal: false,
  },
  onLoad: function () {
  },
  /**
   * 彈窗
   */
  showDialogBtn: function () {
    this.setData({
      showModal: true
    })
  },
  /**
   * 彈出框蒙層截?cái)鄑ouchmove事件
   */
  preventTouchMove: function () {
  },
  /**
   * 隱藏模態(tài)對(duì)話(huà)框
   */
  hideModal: function () {
    this.setData({
      showModal: false
    });
  },
  /**
   * 對(duì)話(huà)框取消按鈕點(diǎn)擊事件
   */
  onCancel: function () {
    this.hideModal();
  },
  /**
   * 對(duì)話(huà)框確認(rèn)按鈕點(diǎn)擊事件
   */
  onConfirm: function () {
    this.hideModal();
  }
})
最后編輯于
?著作權(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ù)。

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