ミニ プログラムを使用してポップアップ ウィンドウ入力コンポーネントを作成する

hzc
リリース: 2020-06-29 10:51:15
転載
2703 人が閲覧しました

プロジェクトを書いているときに、ミニ プログラムには独自のポップアップ入力コンポーネントがないことがわかったので、自分で作成する必要がありました。

1. 半透明のカバー レイヤー

カバー レイヤーのスタイルと表示イベント
wxml コード:

<view class="body">
  <button bindtap=&#39;eject&#39;>弹窗</button>
</view>
<view class="model" catchtouchmove=&#39;preventTouchMove&#39; wx:if=&#39;{{showModal}}&#39;></view>
ログイン後にコピー

wxss コード:

.model{
  position: absolute;
  width: 100%;
  height: 100%;
  background: #000;
  z-index: 999;
  opacity: 0.5;
  top: 0;
  left:0;
}
ログイン後にコピー

js コード:

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

  /**
   * 控制遮盖层的显示
   */
  eject:function(){
    this.setData({
      showModal:true
    })
  }
ログイン後にコピー
2. ポップアップ ウィンドウの実装

ポップアップ ウィンドウのスタイルと表示/非表示のイベント
wxml コード:

<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 コード:

.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 コード:

/**
   * 页面的初始数据
   */
  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. 完全なコード

最後に、完全なコードを示します。少し冗長なので、できるだけ詳しく説明します
wxml コード:

<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 コード:

.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 コード:

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
    })
  }
})
ログイン後にコピー

推奨チュートリアル:「WeChat ミニ プログラム

以上がミニ プログラムを使用してポップアップ ウィンドウ入力コンポーネントを作成するの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:jianshu.com
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!