使用小程式寫一個彈跳窗輸入組件

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
    })
  }
})
登入後複製

推薦教學:《微信小程式》 

以上是使用小程式寫一個彈跳窗輸入組件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:jianshu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!