关于在uni-app中键盘弹出页面内容被顶走的问题和内容总是保持在底部

关于在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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值