ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-01-07 14:31:21  阅读:203  来源: 互联网

标签:body box 输入法 scrollIntoView 遮挡 height input border


解决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

标签:body,box,输入法,scrollIntoView,遮挡,height,input,border
来源: https://www.cnblogs.com/riverone/p/15774899.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有