关于在uni-app中键盘弹出页面内容被顶走的问题和内容总是保持在底部
在此之前找了很多文档看了对键盘的处理很多都是利用了input上的聚焦
focus
和去焦
blur
事件来控制input的移动但是使用这个会导致input框显示有延迟,后面找了一下文档中提供了监控键盘高度变化的事件
uni.onKeyboardHeightChange
,使用该事件可以解决输入框移动延迟的问题,代码如下:
<template>
<view class="continerArea">
<scroll-view class="content_area" scroll-y="true" :scroll-into-view="bottomNum" :scroll-with-animation="true">
<view class="conten_message" v-for="(ite,ind) in dataList" :key="'listMssage'+ind" :id="'scroll-view-'+ind">
text{{ite}}
</view>
</scroll-view>
<view class="send_message" :style="{bottom:bottomVal+'px'}">
<input class="uni-input" type="text" :adjust-position="false" v-model="sendMessage" confirm-type="send" />
</view>
</view>
</template>
<script setup>
import {
ref,
onMounted,
reactive
} from 'vue'
import {
onShow,
onHide
} from "@dcloudio/uni-app"
let sendMessage = ref('')
let dataList = ref([])
let timmer = ref(null)
let bottomNum = ref()
const bottomVal = ref(0)
// 定位当前最后一行文本的位置
const roundTo = () => {
const index = dataList.value.length - 1
bottomNum.value = "scroll-view-" + index
}
// 首次加载
onShow(() => {
let useTime = 0
timmer.value = setInterval(function() {
dataList.value.push(useTime)
useTime += 1
roundTo()
}, 1000);
// 监听键盘高度变化
uni.onKeyboardHeightChange((res) => {
console.log(res)
if (res.height !== 0) {
bottomVal.value = res.height - 82//我在使用时需要减82像素,要不然输入框会离键盘很远,具体看效果吧
} else {
bottomVal.value = res.height
}
})
})
// 页面关闭调用
onHide(() => {
clearInterval(timmer.value)
// 取消监听键盘高度变化
uni.offKeyboardHeightChange((res) => {})
})
</script>
<style lang="less">
.continerArea {
width: 100%;
height: 100vh;
overflow: hidden;
box-sizing: border-box;
padding-top: 10px;
position: relative;
top: 0;
left: 0;
}
.content_area {
width: 100%;
height: calc(100% - 50px);
background-color: #fff;
}
.send_message {
position: absolute;
z-index: 199;
left: 0;
bottom: 0;
height: 50px;
width: 100%;
background-color: #e1e1e1;
flex-shrink: 0;
transition: all 430ms ease;
box-sizing: border-box;
padding: 7px 10px;
.uni-input {
box-sizing: border-box;
width: 100%;
height: 100%;
background-color: #ffffff;
border-radius: 5px;
padding-left: 10px;
}
}
</style>