uniapp开发微信小程序解决点击表单时页面上移问题

当用uniapp开发微信小程序实现用户输入表单时ios点击底部区域会出现页面上移


这个问题好像只有ios会出现,安卓设置input属性adjust-position为true即可,可判断当前手机是ios还是安卓动态设置adjust-position

具体实现

html内容 注意不同输入框绑定的id要不一致
<input id="form-input-0" @keyboardheightchange="bindkeyboardheightchange" :adjust-position="false" @blur="scrollToInput(0)" type="text" v-model="text" />

// 页面底部添加  pageScrollHeight为要滚动的高度
<view :style="{height: pageScrollHeight + 'px'}"></view>

js内容
// 监听页面软键盘弹起手动推动页面
bindkeyboardheightchange(e) {
	// 获取键盘高度
	const height = e.detail.height;
	const className = e.target.id;
	if (height < 1) {
		this.scrollToInput(0);
		return;
	}
	try {
		// 获取输入框底部距离手机顶部距离
		this.createSelectorQuery()
		.select(`#${className}`)
		.boundingClientRect((res) => {
			this.getWindowHeight().then(windowInfo=>{
				// 获取手机高度
				const windowHeight = windowInfo.windowHeight;
				// 除去键盘的剩余高度 得到可视区域高度
				let restHeight = windowHeight - height;
				// 元素左下角坐标
				let bottom = res.bottom + 20;
				bottom = (Math.ceil(bottom * 100) / 100).toFixed(2)
				// 只有当元素被软键盘覆盖的时候才上推页面
				if (bottom <= restHeight) return;
				// 现阶段需要滚动的大小
				let scrollTop = bottom - restHeight;
				this.scrollToInput(height, scrollTop);
			}).catch(error=>{
				console.log(error,"error");
				const windowHeight = 500;
				// 除去键盘的剩余高度
				let restHeight = windowHeight - height;
				// 元素左下角坐标
				let bottom = res.bottom;
				// 只有当元素被软键盘覆盖的时候才上推页面
				if (bottom <= restHeight) return;
				// 现阶段需要滚动的大小
				let scrollTop = bottom - restHeight;
				this.scrollToInput(height, scrollTop);
			})
			
		})
		.exec();
	} catch (error) {}
},
// 获取页面滚动条位置
getScrollOffset() {
	return new Promise((resolve) => {
		try {
			wx.createSelectorQuery()
			.selectViewport()
			.scrollOffset((res) => {
				resolve(res.scrollTop);
			})
			.exec();
		} catch (error) {
			console.log(error,"error");
			resolve(0);
		}
	});
},
// 监听页面软键盘弹起手动推动页面
scrollToInput(keyboardHeight, scrollTop) {
	let that = this;
	if(keyboardHeight === 0){
		this.pageScrollHeight = 0;
	}
	// 浏览器高度 800
	// 键盘高度 270
	// 输入框底部距离手机顶部距离 
	if (scrollTop) {
		try {
			this.getScrollOffset().then((lastScrollTop) => {
				let scrollHeight = parseFloat(lastScrollTop ? lastScrollTop + scrollTop : scrollTop).toFixed(2);
				that.pageScrollHeight = scrollHeight;
				that.$nextTick(()=>{
					uni.pageScrollTo({
						scrollTop: scrollHeight, // 滚动到顶部,可以设置为其他高度
						duration: 0, // 滚动动画时长,单位为毫秒
						success() {
							console.log('滚动成功');
						},
						fail(err) {
							console.error('滚动失败', err);
						}
					});
				})
			});
		} catch (error) {}
	}
}

其他问题

1、如果页面底部有留白会导致不滚动或滚动高度较少

// 更改底部动态style
<view :style="{height: pageScrollHeight ? 'calc(100vh + ' + pageScrollHeight + 'px)' : '0px'}"></view>

2、uni.pageScrollTo不滚动
我遇到这个问题是因为页面高度问题,设置高度即可

height: auto !important;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Uniapp开发微信小程序,可以使用 Vue 的表单组件来实现表单界面,并且可以结合 HTML5 的表单验证属性来实现表单验证。 首先,在页面的 template 中定义表单组件,可以使用 `<form>` 标签包裹表单元素,并使用 `v-model` 指令绑定数据: ```html <template> <form @submit="submitForm"> <input type="text" v-model="formData.username" placeholder="用户名" required /> <input type="password" v-model="formData.password" placeholder="密码" required /> <button type="submit">提交</button> </form> </template> ``` 然后,在页面的 script 中定义表单数据和提交方法: ```javascript <script> export default { data() { return { formData: { username: '', password: '' } }; }, methods: { submitForm() { // 表单验证通过后的逻辑处理 if (this.$refs.form.checkValidity()) { // 表单验证通过,执行提交操作 // 可以在这里调用接口进行数据提交等操作 console.log(this.formData); } } } }; </script> ``` 在 `submitForm` 方法中,使用 `this.$refs.form.checkValidity()` 来检查表单的有效性。如果表单验证通过,则可以执行相应的操作,例如调用接口进行数据提交。 需要注意的是,上述示例中的验证只是基本的前端验证,为了保证数据的安全性,后端也需要进行相应的验证。 另外,还可以使用其他 Vue 插件或自定义指令来实现更复杂的表单验证,例如 VeeValidate、Vuelidate 等。这些插件提供了丰富的验证规则和验证消息的配置选项,可以根据具体需求选择使用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值