Heim  >  Artikel  >  WeChat-Applet  >  Verwenden Sie ein Miniprogramm, um eine Popup-Fenster-Eingabekomponente zu schreiben

Verwenden Sie ein Miniprogramm, um eine Popup-Fenster-Eingabekomponente zu schreiben

hzc
hzcnach vorne
2020-06-29 10:50:042706Durchsuche

Als ich das Projekt schrieb, stellte ich fest, dass das Miniprogramm keine eigene Popup-Eingabekomponente hatte, also musste ich selbst eine schreiben.

1. Durchscheinende Deckschicht

Stil und sichtbare Ereignisse der Deckschicht
wxml-Code:

<view class="body">
  <button bindtap=&#39;eject&#39;>弹窗</button>
</view>
<view class="model" catchtouchmove=&#39;preventTouchMove&#39; wx:if=&#39;{{showModal}}&#39;></view>

wxss-Code:

.model{
  position: absolute;
  width: 100%;
  height: 100%;
  background: #000;
  z-index: 999;
  opacity: 0.5;
  top: 0;
  left:0;
}

js-Code :

 /**
   * 页面的初始数据
   */
  data: {
    showModal: false,
  },

  /**
   * 控制遮盖层的显示
   */
  eject:function(){
    this.setData({
      showModal:true
    })
  }
2. Popup-Fenster-Implementierung

Stil und sichtbare Ereignisse des Popup-Fensters
wxml-Code:

<view class="modalDlg" catchtouchmove=&#39;preventTouchMove&#39; wx:if=&#39;{{showModal}}&#39;>
  <view class=&#39;windowRow&#39;>
    <text class=&#39;userTitle&#39;>标题
  </text>
    <view class=&#39;back&#39; bindtap=&#39;back&#39;>
      返回
    </view>
  </view>
  <view class=&#39;wishName&#39;>
    <input bindinput=&#39;wish_put&#39; placeholder=&#39;请输入内容&#39; class=&#39;wish_put&#39;></input>
  </view>
  <view class=&#39;wishbnt&#39;>
    <button class=&#39;wishbnt_bt&#39; bindtap=&#39;ok&#39;>确定</button>
  </view>
</view>

wxss-Code:

.modalDlg{
  width: 70%;
  position: fixed;
  top:350rpx;
  left: 0;
  right: 0;
  z-index: 9999;
  margin: 0 auto;
  background-color: #fff;
  border-radius: 10rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.windowRow{
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  height: 110rpx;
  width: 100%;
}

.back{
  text-align: center;
  color: #f7a6a2;
  font-size: 30rpx;
  margin: 30rpx;
}

.userTitle{
  font-size: 30rpx;
  color: #666;
  margin: 30rpx;
}

.wishName{
  width: 100%;
  justify-content: center;
  flex-direction: row;
  display: flex;
  margin-bottom: 30rpx;
}

.wish_put{
  width: 80%;
  border: 1px solid;
  border-radius: 10rpx;
  padding-left: 10rpx;
}

.wishbnt{
  width: 100%;
  font-size: 30rpx;
  margin-bottom: 30rpx;
}


.wishbnt_bt{
  width: 50%;
  background-color: #f7a6a2;
  color: #fbf1e8;
  font-size: 30rpx;
  border: 0;
}

js-Code:

/**
   * 页面的初始数据
   */
  data: {
    showModal: false,
    textV:''
  },

  /**
   * 控制显示
   */
  eject:function(){
    this.setData({
      showModal:true
    })
  },

  /**
   * 点击返回按钮隐藏
   */
  back:function(){
    this.setData({
      showModal:false
    })
  },

  /**
   * 获取input输入值
   */
  wish_put:function(e){
    this.setData({
      textV:e.detail.value
    })
  },

  /**
   * 点击确定按钮获取input值并且关闭弹窗
   */
  ok:function(){
    console.log(this.data.textV)
    this.setData({
      showModal:false
    })
  }
3. Vollständiger Code

Zum Schluss präsentiere ich den vollständigen Code, er ist etwas ausführlich, ich möchte so detailliert wie möglich sein
wxml-Code:

<view class="body">
  <button bindtap=&#39;eject&#39;>弹窗</button>
</view>
<view class="model" catchtouchmove=&#39;preventTouchMove&#39; wx:if=&#39;{{showModal}}&#39;></view>
<view class="modalDlg" catchtouchmove=&#39;preventTouchMove&#39; wx:if=&#39;{{showModal}}&#39;>
  <view class=&#39;windowRow&#39;>
    <text class=&#39;userTitle&#39;>标题
  </text>
    <view class=&#39;back&#39; bindtap=&#39;back&#39;>
      返回
    </view>
  </view>
  <view class=&#39;wishName&#39;>
    <input bindinput=&#39;wish_put&#39; placeholder=&#39;请输入内容&#39; class=&#39;wish_put&#39;></input>
  </view>
  <view class=&#39;wishbnt&#39;>
    <button class=&#39;wishbnt_bt&#39; bindtap=&#39;ok&#39;>确定</button>
  </view>
</view>

WXSS-Code:

.body{
  width: 100%;
  height: 100%;
  background-color: #fff;
  position: fixed;
  display: flex;
}

.body button{
  height: 100rpx;
}

.model{
  position: absolute;
  width: 100%;
  height: 100%;
  background: #000;
  z-index: 999;
  opacity: 0.5;
  top: 0;
  left:0;
}

.modalDlg{
  width: 70%;
  position: fixed;
  top:350rpx;
  left: 0;
  right: 0;
  z-index: 9999;
  margin: 0 auto;
  background-color: #fff;
  border-radius: 10rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.windowRow{
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  height: 110rpx;
  width: 100%;
}

.back{
  text-align: center;
  color: #f7a6a2;
  font-size: 30rpx;
  margin: 30rpx;
}

.userTitle{
  font-size: 30rpx;
  color: #666;
  margin: 30rpx;
}

.wishName{
  width: 100%;
  justify-content: center;
  flex-direction: row;
  display: flex;
  margin-bottom: 30rpx;
}

.wish_put{
  width: 80%;
  border: 1px solid;
  border-radius: 10rpx;
  padding-left: 10rpx;
}

.wishbnt{
  width: 100%;
  font-size: 30rpx;
  margin-bottom: 30rpx;
}


.wishbnt_bt{
  width: 50%;
  background-color: #f7a6a2;
  color: #fbf1e8;
  font-size: 30rpx;
  border: 0;
}

JS-Code:

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showModal: false,
    textV:''
  },

  /**
   * 控制显示
   */
  eject:function(){
    this.setData({
      showModal:true
    })
  },

  /**
   * 点击返回按钮隐藏
   */
  back:function(){
    this.setData({
      showModal:false
    })
  },

  /**
   * 获取input输入值
   */
  wish_put:function(e){
    this.setData({
      textV:e.detail.value
    })
  },

  /**
   * 点击确定按钮获取input值并且关闭弹窗
   */
  ok:function(){
    console.log(this.data.textV)
    this.setData({
      showModal:false
    })
  }
})

Empfohlenes Tutorial: „WeChat Mini Program

Das obige ist der detaillierte Inhalt vonVerwenden Sie ein Miniprogramm, um eine Popup-Fenster-Eingabekomponente zu schreiben. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:jianshu.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen