【小程序】点击评论弹出输入框,自定义输入框,防止遮盖。

一、目的

解决输入框被遮盖问题

自定义输入框样式

二、前期效果

.commentInputView{
  width: 100%;
  background-color: rgba(204, 204, 204, 0.61);
  padding: 4%;
  position: fixed;
  display: flex;
  justify-content: space-between;
  bottom: 0;
}

要点:

 

style='bottom:{{height == ""?0:height}}px;'

这里需要靠input组件中的聚焦事件来获取高度,从而让输入框的高度随之变化并出现。

相关JS:

inputFocus(e) {
    console.log(e, '键盘弹起')
    this.setData({
      height: e.detail.height,
      isInput: true
    })
  },

手机上具体效果如下:

三、完整代码:

xml

<text style='background-color:#ccc;' bindtap='focusButn'>这是一条评论</text>

<view class='commentInputView' style='bottom:{{height == ""?0:height}}px;' hidden='{{!isInput}}'>

  <view  class='commentInput'>
    <input class='input' maxlength='-1' value="{{value}}" bindfocus="inputFocus" focus='{{focusInput}}' bindblur="inputBlur"></input>
  </view>
  <button class='send'>发送</button>
</view>

JS:

// pages/input/input.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    focusInput: false,
    height: '',
    isInput: false
  },
  inputFocus(e) {
    console.log(e, '键盘弹起')
    this.setData({
      height: e.detail.height,
      isInput: true
    })
  },
  inputBlur() {
    console.log('键盘收起')
    this.setData({
      isInput: false
    })
  },

  focusButn: function () {
    this.setData({
      focusInput: true,
      isInput: true
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

CSS:

/* pages/input/input.wxss */
page{
  width: 100%;
}
.commentInputView{
  width: 100%;
  height: 100rpx;
  background-color: rgba(204, 204, 204, 0.61);
  padding: 4%;
  position: fixed;
  display: flex;
  justify-content: space-between;
}
.commentInput{
  width:70%;
  background-color:#fff;
  font-size:28rpx;
  border-radius:15rpx;
  display: flex;
  align-items: center;
}

.input{
  width:100%;
  padding: 15rpx;
  font-size: 30rpx;
  margin: 0 auto;
  overflow: hidden;
  line-height-step:20rpx;
  border: 0;
}
.send{
  width: 20%;
  height: 80rpx;
  color: #fff;
  font-size: 34rpx;
  background-color: green;
  border: 0;
}

具体显示没有问题,就是第一次的时候,因为键盘弹起的速度比较快,所以第一次就会*2的高度

这样我们可以用一个缓存来记录手机的键盘高度,当然这是在有之前输入文字操作的情况下才能缓存得了。

 

 

但是实际结果就是在键盘的高度上,因为他只是隐藏了

不过效果还是有的

以后想到更好的解决方案再来分享

 

要在微信小程序弹出输入框,您需要执行以下步骤: 1. 在wxml文件中,您需要添加一个按钮或其他交互元素,当用户点击它时,输入框弹出。 ``` <button bindtap="showInput">点击输入</button> ``` 2. 在js文件中,您需要创建一个函数来显示输入框。您可以使用微信小程序中的`showModal`函数来显示输入框。 ``` Page({ data: { inputValue: '' }, showInput: function() { wx.showModal({ title: '请输入', content: '', showCancel: true, success: function(res) { if (res.confirm) { console.log('用户点击确定') } else if (res.cancel) { console.log('用户点击取消') } } }) } }) ``` 3. 在这个函数中,您可以设置输入框的标题和内容。当用户点击“确定”时,您可以在回调函数中获取输入框中的值,然后将其存储在数据中。 ``` Page({ data: { inputValue: '' }, showInput: function() { wx.showModal({ title: '请输入', content: '', showCancel: true, success: function(res) { if (res.confirm) { console.log('用户点击确定') console.log('输入的值为: ' + res.inputValue) this.setData({ inputValue: res.inputValue }) } else if (res.cancel) { console.log('用户点击取消') } } }) } }) ``` 4. 最后,您可以在wxml文件中使用数据绑定来显示输入框中的值。 ``` <button bindtap="showInput">点击输入</button> <text>您输入的值为: {{inputValue}}</text> ``` 这样,当用户点击按钮时,输入框弹出,并且输入框中的值将显示在页面上。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值