微信小程序 - - - input和键盘一起上弹如何实现?

效果展示:

在这里插入图片描述



方案一: 获取键盘高度 + 设置fixed属性 + 动态bottom

该方式需要通过调用对应API获取键盘高度,通过js动态设置fixed的bottom属性支实现

1. 布局调整

inputBottom即为input需要移动的距离

  <view class="comment_container" style="bottom:{{inputBottom}}px">
      <input type="text" value="{{value}}" placeholder="喜欢这个宝贝就聊聊吧~"  confirm-type='send' bindfocus='focusComment' bindblur='blurComment' adjust-position='{{false}}' />
  </view>

关于获取键盘高度的api,微信提供了好几个,可根据自己的实际情况进行修改,具体可查看:Input

  • bindkeyboardheightchange
  • bindfocus

2. 对应方法


// 失焦事件
blurComment(e) {
  this.setData({
  	inputBottom: 0
  })
},

// 聚焦事件
focusComment(e) {
    const _this = this;
    
    // height 为获取到的键盘的高度
    const {
      height,
    } = e.detail
    
    wx.getSystemInfo({
      success: function (res) {
      
         _this.setData({
            inputBottom: height,
            keyboardHeight: height,
            // keyboardHeight为存储的键盘高度,后续如果不想再获取键盘高度可使用该值
         })
         
        // 如果发现ios和安卓不满意,可自行配置需要逻辑
        if (res.platform == 'ios') { 
          // ios逻辑
        }else{
          // android逻辑
        }
      }
    })
  },


方案二: adjust-position && cursor-spacing 联合使用

该方式仅需设置adjust-position && cursor-spacing两个属性即可实现

实现代码

<input type="text" value="{{value}}" placeholder="喜欢这个宝贝就聊聊吧~"  confirm-type='send' adjust-position='{{true}}' cursor-spacing='20' />

在这里插入图片描述
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值