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

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

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

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

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

所以应用scrollIntoView 的时候并不一定会滚动到顶端和底端。

解决input被遮挡的问题可以先点击此处查看 例子 ,或者直接用手机扫描二维码:
input被遮挡问题的处理例子

从例子中可以看到,每次点击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>
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值