uniapp 内嵌 webview 客服网页,呼出键盘遮挡输入框问题解决记录

有很多情况需要在app端内嵌一个H5客服网页,但这个页面一般都是有打字的需求,但由于大部分情况下网页都是默认铺满整个画面,导致键盘弹出时出现遮挡输入框的问题。

直接上代码:

<template>
	<view class="service-container">
		<view class="content">
			// 这里正常引入webview
			<web-view :src="url">
			</web-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				url: '',  // 网址
				height: 0, // 页面高度
				barHeight: 0, // 状态栏高度
				kbHeight: 0 // 键盘高度
			};
		},
		onLoad() {
			
			this.url = ‘xxxxxxx’;
			// 这里是为了用原生的导航栏遮挡H5客服页面的头部,因为大部分客服页面都没有返回键
				setTimeout(() => {
					uni.setNavigationBarTitle({
						title: '在线客服'
					})
				}, 1000)
				console.log(this.url);
		},
		onShow() {
			//  监听键盘高度变化
			uni.onKeyboardHeightChange((obj) => {
				// 获取系统信息
				let _sysInfo = uni.getSystemInfoSync();
				let _heightDiff = _sysInfo.screenHeight - _sysInfo.windowHeight
				let _diff = obj.height - _heightDiff
				// 键盘高度
				this.kbHeight = (_diff > 0 ? _diff : 0) - 2;
			})
		// 同时监听页面变化
			uni.onWindowResize(res => {
				console.log(res);
				if (res.size.windowHeight < this.height) {
					setTimeout(() => {
						this.wv.setStyle({
							top: this.barHeight,
							// webview的高度动态修改为减去键盘高度后的
							height: this.height - this.kbHeight,
							bottom: 0
						})
					}, 100)
				} else {
					setTimeout(() => {
						this.wv.setStyle({
							top: this.barHeight,
							// 这里可以根据自己情况微调
							height: this.height + 60,
							bottom: 0
						})
					}, 100)
				}
			})
		},
		onReady() {
			// 首次进入页面时,webview高度 = 整个页面高度
			let currentWebview = this.$scope.$getAppWebview();
			uni.getSystemInfo({
				success: res => {
					this.height = res.windowHeight;
					setTimeout(() => {
						this.wv = currentWebview.children()[0];
						this.wv.setStyle({
							top: this.barHeight,
							height: this.height + 60,
							bottom: 0
						});
					}, 100);
				}
			});
		}
	};
</script>

<style lang="scss">
	.service-container {
		background-color: #f2f5ff;
	}
</style>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

万符网络

走过路过,欢迎打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值