项目场景:uniapp多个输入框弹出软键盘后无法滚动到底部|遮挡底部input
在做uniapp 的时候遇到多个输入框的情况,用官方demo做个演示
adjustPan 模式
问题描述
uniapp 多个输入框 无法拉到最下面
原因分析:
1.adjustPan 模式下 软键盘弹出时,webview窗体高度不变,但窗体上推,以保证输入框不被软键盘盖住,此时软键盘会覆盖后面的输入框导致无法展示全部
通过 onKeyboardHeightChange 监听键盘弹出设置外边距为软键盘弹出的高度解决了此问题
2.但是这样会导致鼠标点击上方的时候显示正常,鼠标点击下方的时候 会超出一部分
a.通过设置 adjust-position 可以解决这个问题,但是效果不太好
b.应该是软键盘自动推起输入框的时候 默认加了一个边距,所以需要动态设置 下边距 减去默认推起的高度
解决方案:
最外层 view 设置 下边距 并增加一个点击方法
<view class="nvue-page-root" :style="'padding-bottom:' + dom_height + 'px;'" @click="input_click" id="body">
获取页面高度
onReady() {
var _this = this;
this.window_height = uni.getSystemInfoSync().windowHeight;
const query = uni.createSelectorQuery().in(this);
query.select('#body').boundingClientRect(data => {
console.log("得到布局位置信息" + JSON.stringify(data));
_this.height = data.height
console.log("节点离页面顶部的距离为" + data.top);
}).exec();
},
点击方法
input_click(e){
var _this = this;
uni.onKeyboardHeightChange(res => {
let h = res.height
if(h > 0){
let h2 = e.target.y - (_this.window_height - h)
if(h2 > 0){
h -= h2
}
}
_this.$set(_this, 'dom_height', h);
})
},
完整代码
<template>
<view class="nvue-page-root" :style="'padding-bottom:' + dom_height + 'px;'" @click="input_click" id="body">
<view class="page-title">
<view class="page-title__wrapper">
<text class="page-title__text">{{title}}</text>
</view>
</view>
<view class="uni-common-mt">
<view class="uni-form-item uni-column">
<view class="title"><text class="uni-form-item__title">可自动聚焦的 input</text></view>
<view class="uni-input-wrapper">
<input class="uni-input" placeholder="自动获得焦点" @click="input_click"/>
</view>
</view>
<!-- #ifdef APP-PLUS -->
<view v-if="platform==='ios'&&!isNvue" class="uni-form-item uni-column">
<view class="title"><text class="uni-form-item__title">隐藏 iOS 软键盘上的导航条</text></view>
<view class="uni-input-wrapper">
<input class="uni-input" placeholder="触摸其他地方收起键盘" @focus="onFocus" @blur="onBlur" @click="input_click"/>
</view>
</view>
<!-- #endif -->
<view class="uni-form-item uni-column">
<view class="title"><text class="uni-form-item__title">键盘右下角按钮显示为搜索</text></view>
<view class="uni-input-wrapper">
<input class="uni-input" confirm-type="search" placeholder="键盘右下角按钮显示为搜索" @click="input_click"/>
</view>
</view>
<!-- #ifndef H5 -->
<view class="uni-form-item uni-column">
<view class="title"><text class="uni-form-item__title">键盘右下角按钮显示为发送</text></view>
<view class="uni-input-wrapper">
<input class="uni-input" confirm-type="send" placeholder="键盘右下角按钮显示为发送" @click="input_click"/>
</view>
</view>
<!-- #endif -->
<view class="uni-form-item uni-column">
<view class="title"><text class="uni-form-item__title">控制最大输入长度的 input</text></view>
<view class="uni-input-wrapper">
<input class="uni-input" maxlength="10" placeholder="最大输入长度为10" @click="input_click"/>
</view>
</view>
<view class="uni-form-item uni-column">
<view class="title"><text class="uni-form-item__title">实时获取输入值:{{inputValue}}</text></view>
<view class="uni-input-wrapper">
<input class="uni-input" @input="onKeyInput" placeholder="输入同步到view中" @click="input_click"/>
</view>
</view>
<view class="uni-form-item uni-column">
<view class="title"><text class="uni-form-item__title">控制输入的 input</text></view>
<view class="uni-input-wrapper">
<input class="uni-input" @input="replaceInput" v-model="changeValue" placeholder="连续的两个1会变成2" :adjust-position="false" />
</view>
</view>
<!-- #ifndef MP-BAIDU -->
<view class="uni-form-item uni-column">
<view class="title"><text class="uni-form-item__title">控制键盘的 input</text></view>
<view class="uni-input-wrapper">
<input class="uni-input" ref="input1" @input="hideKeyboard" placeholder="输入123自动收起键盘" :adjust-position="false"/>
</view>
</view>
<!-- #endif -->
<view class="uni-form-item uni-column">
<view class="title"><text class="uni-form-item__title">数字输入的 input</text></view>
<view class="uni-input-wrapper">
<input class="uni-input" type="number" placeholder="这是一个数字输入框"/>
</view>
</view>
<view class="uni-form-item uni-column">
<view class="title"><text class="uni-form-item__title">密码输入的 input</text></view>
<view class="uni-input-wrapper">
<input class="uni-input" password type="text" placeholder="这是一个密码输入框" @click="input_click"/>
</view>
</view>
<view class="uni-form-item uni-column">
<view class="title"><text class="uni-form-item__title">带小数点的 input</text></view>
<view class="uni-input-wrapper">
<input class="uni-input" type="digit" placeholder="带小数点的数字键盘" @click="input_click"/> </view>
</view>
<view class="uni-form-item uni-column">
<view class="title"><text class="uni-form-item__title">身份证输入的 input</text></view>
<view class="uni-input-wrapper">
<input class="uni-input" type="idcard" placeholder="身份证输入键盘" @click="input_click"/> </view>
</view>
<view class="uni-form-item uni-column">
<view class="title"><text class="uni-form-item__title">控制占位符颜色的 input</text></view>
<view class="uni-input-wrapper">
<input class="uni-input" placeholder-style="color:#F76260" placeholder="占位符字体是红色的" @click="input_click"/>
</view>
</view>
<view class="uni-form-item uni-column">
<view class="title"><text class="uni-form-item__title">带清除按钮的输入框</text></view>
<view class="uni-input-wrapper">
<input class="uni-input" placeholder="带清除按钮的输入框" :value="inputClearValue" @input="clearInput" @click="input_click"/>
<text class="uni-icon" v-if="showClearIcon" @click="clearIcon"></text>
</view>
</view>
<view class="uni-form-item uni-column">
<view class="title"><text class="uni-form-item__title">可查看密码的输入框</text></view>
<view class="uni-input-wrapper">
<input class="uni-input" placeholder="请输入密码" :password="showPassword" @click="input_click"/>
<text class="uni-icon" :class="[!showPassword ? 'uni-eye-active' : '']" @click="changePassword"></text>
</view>
</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
title: 'input',
focus: false,
inputValue: '',
showClearIcon: false,
inputClearValue: '',
changeValue: '',
showPassword: true,
src: '../../../static/eye-1.png',
platform: '',
isNvue: false,
dom_height: 0,
height: 0,
window_height: 0,
}
},
watch: {
dom_height(val, oldval){
console.log(val, oldval)
}
},
onReady() {
var _this = this;
this.window_height = uni.getSystemInfoSync().windowHeight;
const query = uni.createSelectorQuery().in(this);
query.select('#body').boundingClientRect(data => {
console.log("得到布局位置信息" + JSON.stringify(data));
_this.height = data.height
console.log("节点离页面顶部的距离为" + data.top);
}).exec();
},
methods: {
input_click(e){
var _this = this;
uni.onKeyboardHeightChange(res => {
let h = res.height
if(h > 0){
let h2 = e.target.y - (_this.window_height - h)
if(h2 > 0){
h -= h2
}
}
_this.$set(_this, 'dom_height', h);
})
},
onKeyInput: function(event) {
this.inputValue = event.detail.value
},
replaceInput: function(event) {
var value = event.detail.value;
if (value === '11') {
this.changeValue = '2';
}
},
hideKeyboard: function(event) {
if (event.detail.value === '123') {
uni.hideKeyboard();
}
},
clearInput: function(event) {
this.inputClearValue = event.detail.value;
if (event.detail.value.length > 0) {
this.showClearIcon = true;
} else {
this.showClearIcon = false;
}
},
clearIcon: function() {
this.inputClearValue = '';
this.showClearIcon = false;
},
changePassword: function() {
this.showPassword = !this.showPassword;
},
onFocus() {
this.$mp.page.$getAppWebview().setStyle({
softinputNavBar: 'none'
})
},
onBlur() {
this.$mp.page.$getAppWebview().setStyle({
softinputNavBar: 'auto'
})
}
},
onLoad() {
this.platform = uni.getSystemInfoSync().platform
// #ifdef APP-PLUS-NVUE
this.isNvue = true
// #endif
}
}
</script>
<style scoped>
.nvue-page-root {
background-color: #F8F8F8;
padding-bottom: 20px;
}
.page-title {
/* #ifndef APP-NVUE */
display: flex;
/* #endif */
flex-direction: row;
justify-content: center;
align-items: center;
padding: 35rpx;
}
.page-title__wrapper {
padding: 0px 20px;
border-bottom-color: #D8D8D8;
border-bottom-width: 1px;
}
.page-title__text {
font-size: 16px;
height: 48px;
line-height: 48px;
color: #BEBEBE;
}
.title {
padding: 5px 13px;
}
.uni-form-item__title {
font-size: 16px;
line-height: 24px;
}
.uni-input-wrapper {
/* #ifndef APP-NVUE */
display: flex;
/* #endif */
padding: 8px 13px;
flex-direction: row;
flex-wrap: nowrap;
background-color: #FFFFFF;
}
.uni-input {
height: 28px;
line-height: 28px;
font-size: 15px;
padding: 0px;
flex: 1;
background-color: #FFFFFF;
}
.uni-icon {
font-family: uniicons;
font-size: 24px;
font-weight: normal;
font-style: normal;
width: 24px;
height: 24px;
line-height: 24px;
color: #999999;
}
.uni-eye-active {
color: #007AFF;
}
</style>
background-color: #FFFFFF;
}
.uni-icon {
font-family: uniicons;
font-size: 24px;
font-weight: normal;
font-style: normal;
width: 24px;
height: 24px;
line-height: 24px;
color: #999999;
}
.uni-eye-active {
color: #007AFF;
}
</style>