uniapp textarea输入框软键盘遮挡问题

一、首先说下我的页面布局,总盒子高度设置了height:100vh,页面有导航栏,内容滚动区,底部包裹输入框的元素,其中:
       1、导航栏设置固定定位top:0,padding-top为menuButtonBounding.top,height为menuButtonBounding.height;

       2、内容滚动区域设置height:0,flex:1;
       3、底部输入框父元素动态设置height,无定位
二、问题描述:在手机上输入内容,内容多行后软键盘会遮挡内容导致展示不全;
                         之前设置了底部元素一个固定高度150rpx,也监听了软键盘高度变化并设置软键盘高度值,具体代码如下
 

// 监听键盘高度

        keyboardHeightChange() {

            const self = this;

            uni.onKeyboardHeightChange(res => {

                self.keyboardHeight = rpxUtil.rpxTopx(res.height);

                if (self.keyboardHeight <= 0) {

                    self.keyboardHeight = 0;

                }

                self.goBottom();

            })

        },

动态设置底部输入框部分高度为固定高度150 + 软键盘高度 keyboardHeight的值;
问题是textarea设置了:auto-height="true",所以超过一定高度会遮挡

三、问题解决:

       在textarea的换行方法@linechange="listenTextAreaHeight"中监听底部盒子的高度,动态设置高度,具体代码如下:
 

    // 监听发送栏高度

            listenTextAreaHeight() {

                setTimeout(() => {

                    this.getElementHeight()

                }, 10)

            },

            //获取高度方法

            getElementHeight() {

                const query = uni.createSelectorQuery().in(this);

                query.select('.submit').boundingClientRect(data => {

                    this.$emit('heights', data.height);

                }).exec();

            },
由于我这个组件是分开写的所以用到了emit传值,在父组件中拿到这个高度设置底部框高度为该自身高度+软键盘高度即可。

在 Vue3 中,iPhone 的软键盘可能会遮挡页面上的输入框(`<input>` 或 `<textarea>` 等),这是因为 iPhone 的设计使得当软键盘弹出时,屏幕会自动向上滚动以适应。这通常会导致输入框被隐藏。为了解决这个问题,你可以尝试以下几个方法: 1. 使用 CSS 针对 iOS 设备调整样式:通过媒体查询 (`@media screen and (max-height: ...px)`) 来设置当键盘打开时,将输入框相对定位并固定其位置,保持其可见。 ```css @media (max-height: 600px) { .ios-input-fix { position: relative; padding-bottom: env(safe-area-inset-bottom); margin-bottom: -100%; overflow-y: auto; } } ``` 然后,在需要处理的输入元素上应用这个类名 `ios-input-fix`: ```html <input type="text" class="ios-input-fix"> ``` 2. 使用 Vue 的 `v-once` 指令:如果你的输入框内容不需要改变并且希望避免多次渲染,可以使用 `v-once` 防止每次键盘弹出时重新渲染整个组件。 ```html <div v-once> <input type="text" /> </div> ``` 3. 利用 Vue 的 `mounted` 和 `beforeDestroy` 生命周期钩子:在键盘打开时调整输入框的 scrollTop,关闭时恢复原状。 ```javascript export default { mounted() { this.$nextTick(() => { document.addEventListener('keyboardDidShow', () => { this.adjustInputPosition(); }); }); }, beforeDestroy() { document.removeEventListener('keyboardDidShow', this.adjustInputPosition); }, methods: { adjustInputPosition() { // 根据实际需求计算输入框的滚动位置 const inputEl = document.querySelector('.your-input-class'); if (inputEl && !this.keyboardShowing) { inputEl.scrollTop -= this.keyboardHeight; // 假设 keyboardHeight 已经获取到 } }, }, }; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值