uniapp微信公众号密码输入框输入完成后留下空白区域的问题解决方案

本文介绍了一个使用JavaScript解决网页在用户输入密码时因软键盘弹出导致页面布局变化的问题。通过监听focusin和focusout事件,当软键盘关闭时,临时创建一个不可见的input元素来触发滚动恢复。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 键盘问题
			this.bodyHeight = document.documentElement.clientHeight
			
			let timer = null
			document.body.addEventListener('focusin', (e) => { // 软键盘弹起事件
				if (timer && e.target.type !== 'button') {
					clearTimeout(timer)
					timer = null
				}
			})
			
			document.body.addEventListener('focusout', (e) => {
				if (e.target.type === 'password') {
				    timer = setTimeout(() => {
				      clearTimeout(timer)
				      timer = null
				      const nowH = document.documentElement.clientHeight
				      console.log('timeout', nowH, this.bodyHeight)
				      if (nowH < this.bodyHeight) {
				        const oinput = document.createElement('input')
				        oinput.style.width = '0px'
				        document.body.appendChild(oinput)
				        oinput.focus()
				        oinput.blur()
				        document.body.removeChild(oinput)
				      }
				    }, 500)
				}
			}) // 软键盘关闭事件

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值