使用小程序写一个弹窗输入组件

hzc
发布: 2020-06-29 10:51:15
转载
2702 人浏览过

写项目的时候发现小程序没有自带的弹窗输入的组件,只能自己写一个。

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学习者快速成长!