关于移动端输入键盘遮挡页面

本文介绍了一种在移动端网页中解决软键盘遮挡验证码输入框的方法,通过调整登录框的CSS样式,特别是使用fixed布局和设置bottom属性来确保即使在软键盘弹出时也能正常显示验证码。

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

 问题显示:

当吊起手机的软键盘的时候 会遮住验证码输入内容,造成盲输入

 

问题解决:

 

在包裹 登录的父元素样式上加:

.login{
	position: fixed;
	background-color: #fff;
	width: 600px;
	box-sizing: border-box;
	border-radius: 50px;
	margin: 0 auto;
	transform: translatey(50%);
	padding: 40px 70px;
	left: 50%;
	margin-left: -300px;
	bottom: 50%;
}

  重点就是bottom:50% (在希望登录框居中的情况下)

我们可以发现 ,当页面吊起移动端的软键盘的时候 页面的高度发生了改变,使用fixed布局,检测到了改变 并作出了相应的变化,从底部定位 可以确保键盘出现的时候,页面的内容可以正常显示出来。

转载于:https://www.cnblogs.com/GoTing/p/9029272.html

这个问题通常可以通过监听软键盘的打开和关闭事件来解决。可以使用 Vue.js 的 `$nextTick()` 方法和 `window.innerHeight` 属性来获取页面高度和视口高度。具体实现步骤如下: 1. 在 `mounted()` 钩子函数中,添加软键盘打开和关闭事件的监听函数。 ```javascript mounted() { window.addEventListener('resize', this.handleResize) window.addEventListener('scroll', this.handleScroll) window.addEventListener('focusin', this.handleFocusIn) window.addEventListener('focusout', this.handleFocusOut) } ``` 2. 实现监听函数,根据软键盘的状态计算页面高度和视口高度,并更新页面的滚动位置。 ```javascript methods: { handleResize() { if (document.activeElement.tagName === 'INPUT' || document.activeElement.tagName === 'TEXTAREA') { this.isKeyboardOpened = true } else { this.isKeyboardOpened = false } this.updateViewportHeight() }, handleScroll() { if (!this.isKeyboardOpened) return this.updateViewportHeight() }, handleFocusIn() { this.isKeyboardOpened = true this.updateViewportHeight() }, handleFocusOut() { this.isKeyboardOpened = false this.updateViewportHeight() }, updateViewportHeight() { const height = window.innerHeight const input = this.$refs.input const inputHeight = input.offsetHeight const inputOffsetTop = input.getBoundingClientRect().top const offset = window.scrollY const scrollHeight = document.body.scrollHeight const keyboardHeight = height - inputHeight - inputOffsetTop + offset if (keyboardHeight > 0) { document.body.style.height = scrollHeight + keyboardHeight + 'px' document.body.style.overflow = 'hidden' window.scrollTo(0, keyboardHeight) } else { document.body.style.height = '' document.body.style.overflow = '' window.scrollTo(0, 0) } } } ``` 3. 在页面中添加输入框,并绑定 ref 属性。 ```html <template> <div> <input type="text" ref="input"> </div> </template> ``` 这样,当软键盘打开时,页面会自动调整滚动位置,避免输入框被遮挡
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值