解决移动端键盘遮挡输入框问题

方法一:scrollIntoViewIfNeeded:如果已经在可视区域,就不滑动
scrollIntoView:一直都滑动

this.$nextTick(()=>{
        let documentInput = document.getElementsByTagName('input');
        for(let i=0;i<documentInput.length;i++){
          documentInput[i].onclick = function(){
            this.scrollIntoViewIfNeeded(false);
          }
        }
      })
element.scrollIntoView(); // 等同于element.scrollIntoView(true) 
element.scrollIntoView(alignToTop); // Boolean型参数 
element.scrollIntoView(scrollIntoViewOptions); // Object型参数

如果为true,元素的顶端将和其所在滚动区的可视区域的顶端对齐。相应的 scrollIntoViewOptions: {block: “start”, inline: “nearest”}。这是这个参数的默认值。
如果为false,元素的底端将和其所在滚动区的可视区域的底端对齐。相应的scrollIntoViewOptions: {block: “end”, inline: “nearest”}。

scrollIntoViewOptions 可选
官网

其他作者的总结

方法二:
出现当前键盘遮挡输入框事件,可能并不是键盘遮挡,而是当前页面某些定位的元素遮挡了,页面中position:absolute/fixed等等设置的元素,
解决办法:

    this.$nextTick(()=>{
      this.clientHeight  = document.documentElement.clientHeight;
      console.log(document.documentElement.clientHeight, 11111111111)
    })
    window.addEventListener('resize', () => {
        let clientHeight  = document.documentElement.clientHeight ;
        console.log(clientHeight, this.clientHeight, '222222222222222')
        if(clientHeight <this.clientHeight ){
          this.isHidden = true;
        }else{
          this.isHidden = false;
        }
    }) 
在Vue中,解决键盘遮挡输入框问题,可以通过监听软键盘的弹出和隐藏事件,根据键盘的高度动态调整页面布局,使得输入框不被键盘遮挡。具体的实现方法如下: 1. 安装vue-soft-keyboard插件 可以使用vue-soft-keyboard插件来监听软键盘的弹出和隐藏事件,并且动态调整页面布局。可以使用npm安装: ``` npm install vue-soft-keyboard --save ``` 2. 在Vue实例中引入vue-soft-keyboard插件 在Vue实例中引入vue-soft-keyboard插件,并且指定需要监听软键盘事件的输入框的ref属性。 ``` import softKeyboard from 'vue-soft-keyboard' export default { components: { softKeyboard }, data () { return { inputRef: null } }, methods: { onSoftKeyboardShow (e) { // 获取键盘高度并调整输入框位置 const height = e.detail.height this.$refs.inputRef.style.bottom = `calc(100vh - ${height}px)` }, onSoftKeyboardHide () { // 隐藏键盘并恢复输入框位置 this.$refs.inputRef.style.bottom = 0 } } } ``` 3. 在模板中使用vue-soft-keyboard插件 在模板中使用vue-soft-keyboard插件,并且将需要监听软键盘事件的输入框的ref属性绑定到data中的inputRef属性上。 ``` <template> <div> <input ref="inputRef" type="text"> <soft-keyboard :input="inputRef" @show="onSoftKeyboardShow" @hide="onSoftKeyboardHide"></soft-keyboard> </div> </template> ``` 这样,在移动端浏览器中,当用户点击输入框时,软键盘就会弹出,并且vue-soft-keyboard插件也会自动监听软键盘的弹出和隐藏事件,根据键盘的高度动态调整页面布局,使得输入框不被键盘遮挡
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值