解决input获取焦点,弹出输入法之后,input被遮挡的问题

解决input获取焦点,弹出输入法之后,input被遮挡的问题

 

关于input输入框fixed在窗口底部的时候,input获取焦点,弹出输入法,input会被输入法遮挡,导致输入内容不方便。

我们可以用scrollIntoView 与 scrollIntoViewIfNeeded来解决这个问题。scrollIntoView 与 scrollIntoViewIfNeeded都是让当前的元素滚动到浏览器窗口的可视区域内。关于scrollIntoView 与 scrollIntoViewIfNeeded 的具体信息可以查看 此处 。

在MDN的 scrollIntoView 中有下面一段话:

取决于其它元素的布局情况,此元素可能不会完全滚动到顶端或底端。

 


从例子中可以看到,每次点击input输入框之后,输入法会优先弹出,300ms之后将input输入框滚动到浏览器窗口的可视区域内,输入内容之后,点击确定,会在内容区域新增刚刚输入的内容,并且在300ms之后将它滚动到浏览器窗口的可视区域内。

亲测在安卓和ios下的微信6.6.7版本均有效。

例子代码如下:

<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>scrollIntoView</title>
<link rel="stylesheet" href="https://fxss5201.cn/project/css/normalize.css">
<style>
html,
body {
height: 100%;
font-family: "Microsoft YaHei", Arial, Helvetica, "宋体", sans-serif;
font-size: 14px;
user-select: none;
-webkit-touch-callout: none;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}
.clearfix:before,
.clearfix:after {
content: '.';
display: block;
width: 0;
height: 0;
overflow: hidden;
visibility: hidden;
font-size: 0;
line-height: 0;
}
.clearfix:after {
clear: both;
}
.clearfix {
zoom: 1;
}
.header {
position: fixed;
left: 0;
top: 0;
z-index: 10;
width: 100%;
height: 50px;
line-height: 50px;
padding: 0 15px;
box-sizing: border-box;
color: #fff;
background: #999;
}
.body {
padding: 50px 15px;
width: 100%;
overflow-y: scroll;
box-sizing: border-box;
line-height: 30px;
}
.footer {
position: fixed;
left: 0;
bottom: 0;
z-index: 10;
width: 100%;
height: 50px;
padding: 8px 15px 9px;
box-sizing: border-box;
background: #fff;
border-top: 1px solid #ddd;
}
.sure-btn {
padding: 0;
float: right;
margin-left: 10px;
width: 50px;
height: 32px;
color: #fff;
font-size: 14px;
background: #0FAEFF;
border: 0;
border-radius: 3px;
-webkit-appearance: none;
}
.ellipsis {
overflow: hidden;
text-overflow: ellipsis;
white-space: normal;
}
.text-input {
box-sizing: border-box;
width: 100%;
height: 32px;
border-radius: 3px;
padding: 0 6px;
border: 1px solid #ddd;
-webkit-appearance: none;
}
</style>
</head>

<body>
<div class="header">header</div>
<div class="body" id="body">
body</br>
body</br>
body</br>
body</br>
body</br>
body</br>
body</br>
body</br>
body</br>
body</br>
body</br>
body</br>
body</br>
body</br>
body</br>
body</br>
body</br>
body</br>
body</br>
body</br>
body</br>
body</br>
body</br>
body</br>
</div>
<div class="footer" id="footer">
<button class="sure-btn" id="sureBtn">确定</button>
<div class="ellipsis">
<input type="text" class="text-input" id="textInput">
</div>
</div>
<script src="https://fxss5201.cn/project/js/jquery-1.11.3.js"></script>
<script>
$(function () {
$("#textInput").on("focus", function () {
setTimeout(function () {
$("#textInput")[0].scrollIntoView();
$("#textInput")[0].scrollIntoViewIfNeeded();
}, 300);
});

$("#sureBtn").on("click", function () {
var textInputValur = $.trim($("#textInput").val());
$("#textInput").val("").focus();
var body = $("#body");
if (!textInputValur) {
return false;
}
var str = $('<span>' + textInputValur + '</span></br>');
body.append(str);
setTimeout(function () {
str[0].scrollIntoView();
str[0].scrollIntoViewIfNeeded();
}, 300);
})
})
</script>
</body>

</html>
————————————————
版权声明:本文为CSDN博主「樊小书生」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/fxss5201/article/details/80672579

posted @ 2022-01-07 14:24  桂长江  阅读(590)  评论(0编辑  收藏  举报