溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何在微信小程序中自定義彈窗

發(fā)布時(shí)間:2021-06-01 18:14:06 來源:億速云 閱讀:139 作者:Leah 欄目:web開發(fā)

如何在微信小程序中自定義彈窗?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。

業(yè)務(wù)代碼中:

  在業(yè)務(wù)代碼中引入dialog組件即可

<dialog visible="{{dialogVisible}}" showFooter="{{footerVisible}}" title="測試一下">
    <view class='dialog-body' slot="dialog-body">
      <view class='dialog-content'>申請(qǐng)獲取你微信綁定的手機(jī)號(hào)</view>
      <view class='dialog-footer' slot="dialog-footer">
        <button class='cancel-btn' bindtap="close">取消</button>
        <button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber" class='confirm-btn'>授權(quán)</button>
      </view>
    </view>
  </dialog>

dialog組件:

component下面新建dialog。注意是 component 不是 page ,因?yàn)橐鳛榻M件引入到頁面中

  dialog.wxml:

  需要傳入四個(gè)屬性

    visible:是否顯示彈窗

    title :標(biāo)題

    showClose:是否顯示右上角關(guān)閉按鈕

    showFooter:是否顯示底部按鈕

<!--components/dialog/dialog.wxml-->
<view class='dialog-custom' wx:if="{{visible}}">
  <view class='dialog-mask' bindtap="clickMask"></view>
    <view class="dialog-main">
      <view class="dialog-container">
        <view class='dialog-container__title' wx:if="{{title.length>0}}">
          <view class='title-label'>{{ title }}</view>
          <view class='title-icon'>
            <image wx:if="{{showClose}}" bindtap='close' src='/images/close-btn.png'></image>
          </view>
        </view>
      <view class='dialog-container__body'>
        <slot name="dialog-body"></slot>
      </view>
      <view class='dialog-container__footer' wx:if="{{showFooter}}">
        <view class='dialog-container__footer__cancel' bindtap="close">取消</view>
        <view class='dialog-container__footer__confirm' bindtap='confirm'>確定</view>
      </view>
    </view>
  </view>
</view>

dialog.js

Component({
/**
* 組件的屬性列表
*/
properties: {
  visible: {
    type: Boolean,
    value: false
  },
  width: {
    type: Number,
    value: 85
  },
  position: {
    type: String,
    value: 'center'
  },
  title: {
    type: String,
    value: ''
  },
  showClose: {
    type: Boolean,
    value: true
  },
  showFooter: {
    type: Boolean,
    value: false
  },
},
/**
* 組件的初始數(shù)據(jù)
*/
data: {
},
options:{
  multipleSlots: true
},
/**
* 組件的方法列表
*/
methods: {
  clickMask() {
    this.setData({ visible: false });
  },
  close(){
    this.setData({ visible: false });
  },
  cancel() {
    this.setData({ visible: false });
    this.triggerEvent('cancel');
  },
  confirm() {
    this.setData({ visible: false });
    this.triggerEvent('confirm');
  }
}
})

dialog.json:聲明是組件就行 

{
  "component": true,
  "usingComponents": {}
}

dialog.wxss

  css可以根據(jù)自己喜好的樣式調(diào)整,注意mask遮罩層的z-index高一點(diǎn),確保在最上層

/* components/dialog/dialog.wxss */
.dialog-custom {
  width: 100vw;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
  z-index: 9999;
}
.dialog-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10000;
  width: 100vw;
  height: 100%;
  background: rgba(0, 0, 0, 0.3);
}
.dialog-main {
  position: fixed;
  z-index: 10001;
  top: 50%;
  left: 0;
  right: 0;
  width: 85vw;
  height: auto;
  margin: auto;
  transform: translateY(-50%);
}
.dialog-container {
  margin: 0 auto;
  background: #fff;
  z-index: 10001;
  border-radius: 3px;
  box-sizing: border-box;
  padding: 40rpx;
}
.dialog-container__title {
  width: 100%;
  height: 50rpx;
  line-height: 50rpx;
  margin-bottom: 20rpx;
  position: relative;
}
.dialog-container__title .title-label{
  display: inline-block;
  width: 100%;
  height: 50rpx;
  line-height: 50rpx;
  font-size: 36rpx;
  color: #000;
  text-align: center;
}
.dialog-container__title .title-icon{
  width: 34rpx;
  height: 50rpx;
  position: absolute;
  top: 0;
  right: 0;
}
.dialog-container__title .title-icon image{
  width: 34rpx;
  height: 34rpx;
}

.dialog-container__body {
  padding-top: 10rpx;
  font-size: 32rpx;
  line-height: 50rpx;
}

.dialog-container__footer {
  height: 76rpx;
  line-height: 76rpx;
  font-size: 32rpx;
  text-align: center;
  border-top: 1px solid #f1f1f1;
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
}

.dialog-container__footer .dialog-container__footer__cancel {
  width: 50%;
  color: #999;
  display: inline-block;
}
.dialog-container__footer .dialog-container__footer__cancel::after{
  position: absolute;
  right: 50%;
  bottom: 0;
  content: '';
  width: 2rpx;
  height: 76rpx;
  background: #f1f1f1;
}
.dialog-container__footer .dialog-container__footer__confirm {
  color: #3B98F7;
  width: 50%;
  display: inline-block;
  text-align: center;
}
/* components/dialog/dialog.wxss */
.dialog-custom {
width: 100vw;
height: 100%;
position: absolute;
left: 0;
top: 0;
z-index: 9999;
}
.dialog-mask {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 10000;
width: 100vw;
height: 100%;
background: rgba(0, 0, 0, 0.3);
}
.dialog-main {
position: fixed;
z-index: 10001;
top: 50%;
left: 0;
right: 0;
width: 85vw;
height: auto;
margin: auto;
transform: translateY(-50%);
}
.dialog-container {
margin: 0 auto;
background: #fff;
z-index: 10001;
border-radius: 3px;
box-sizing: border-box;
padding: 40rpx;
}
.dialog-container__title {
width: 100%;
height: 50rpx;
line-height: 50rpx;
margin-bottom: 20rpx;
position: relative;
}
.dialog-container__title .title-label{
display: inline-block;
width: 100%;
height: 50rpx;
line-height: 50rpx;
font-size: 36rpx;
color: #000;
text-align: center;
}
.dialog-container__title .title-icon{
width: 34rpx;
height: 50rpx;
position: absolute;
top: 0;
right: 0;
}
.dialog-container__title .title-icon image{
width: 34rpx;
height: 34rpx;
}
.dialog-container__body {
 padding-top: 10rpx;
 font-size: 32rpx;
 line-height: 50rpx;
}
.dialog-container__footer {
 height: 76rpx;
 line-height: 76rpx;
 font-size: 32rpx;
 text-align: center;
 border-top: 1px solid #f1f1f1;
 position: absolute;
 bottom: 0;
 left: 0;
 right: 0;
}
.dialog-container__footer .dialog-container__footer__cancel {
 width: 50%;
 color: #999;
 display: inline-block;
}
.dialog-container__footer .dialog-container__footer__cancel::after{
 position: absolute;
 right: 50%;
 bottom: 0;
 content: '';
 width: 2rpx;
 height: 76rpx;
 background: #f1f1f1;
}
.dialog-container__footer .dialog-container__footer__confirm {
 color: #3B98F7;
 width: 50%;
 display: inline-block;
 text-align: center;
}

關(guān)于如何在微信小程序中自定義彈窗問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI