主页

uniapp/微信小程序input键盘弹出导致页面上移取消方法

念得小柔   IP属地: 青海
0 点赞 0阅读 2493
通过获取键盘高度,在使用绝对定位控制bottom高度控制容器所在位置,这样输入框就一直在键盘的上方并且页面不会跟随键盘高度而上移了
上代码
<view id="comment" :style="'bottom:'+changeBottomVal">
			<image :src="userInfo.avatar?userInfo.avatar:'/static/images/renwu.png'" class="img-t"></image>
			<input type="text" placeholder="我来评论" v-model="content" @focus="inputBindFocus"   @blur="inputBindBlur" :adjust-position="false" />
			<text class="send-btn" @click="sendBtn">发送</text>
		</view>


/*  */
	#comment {
		position: fixed;
		bottom: 20rpx;
		left: 0;
		width: 82%;
		margin: 0 50rpx;
		background: #F2F2F4;
		padding: 10rpx 20rpx;
		border-radius: 40rpx;
	}

	.img-t {
		width: 70rpx;
		height: 70rpx;
		border-radius: 50rpx;
		float: left;
	}

	#comment input {
		float: left;
		line-height: 21px;
		height: 70rpx;
		font-size: 30rpx;
		margin-left: 10rpx;
	}

	.send-btn {
		float: right;
		margin-right: 15rpx;
		line-height: 70rpx;
		font-size: 30rpx;
		color: #298815
	}
export default {
		data() {
			return {
				
				changeBottomVal:'20rpx',
			}
		},
		
		
		methods: {
			inputBindFocus(e) {
					// 获取手机键盘的高度,赋值给input 所在盒子的 bottom 值
					// 注意!!! 这里的 px 至关重要!!! 我搜到的很多解决方案都没有说这里要添加 px
					console.log(e)
					this.changeBottomVal = e.detail.height + 'px'
				},
			   
				inputBindBlur() {
					// input 失去焦点,键盘隐藏,设置 input 所在盒子的 bottom 值为0
					this.changeBottomVal = 20+'rpx';
				},
				
		}
	}









© 著作权归作者所有,转载或内容合作请联系作者