首页 > 其他分享 >解决input获取焦点,弹出输入法之后,input被遮挡的问题

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

时间:2023-11-13 19:01:37浏览次数:31  
标签:body box 输入法 scrollIntoView 遮挡 height input border

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

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

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

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

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

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

从例子中可以看到,每次点击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>

标签:body,box,输入法,scrollIntoView,遮挡,height,input,border
From: https://blog.51cto.com/u_16281588/8351815

相关文章

  • Readonly只针对input(text/password)和textarea有效,而disabled对于所有的表单元素有效
    Readonly只针对input(text/password)和textarea有效,而disabled对于所有的表单元素有效,包括select,radio,checkbox,button等Readonly和Disabled是用在表单中的两个属性,它们都能够做到使用户不能够更改表单域中的内容。但是它们之间有着微小的差别,总结如下:Readonly只针对input(tex......
  • input type="number" 时去除上下按钮样式
    全局样式/*取消[type='number']的input的上下箭头*/input::-webkit-inner-spin-button{-webkit-appearance:none!important;}input::-webkit-outer-spin-button{-webkit-appearance:none!important;}input[type="number"]{-moz-appearance......
  • vue指令实现input自动聚焦
    vue指令实现自动聚焦代码如下:AutoFocus.jsimportVuefrom'vue'//插件对象(必须有install方法,才可以注入到Vue.use中)exportdefault{install(){Vue.directive('fofo',{inserted(el){fn(el)},update(el){fn(el)......
  • 无涯教程-批处理 - Batch Files – Inputs函数
    运行批处理文件时,它使您可以选择传递命令行参数,然后可以在程序中读取它们以进行进一步处理,可以使用%运算符以及参数的数字位置从程序中调用批处理文件参数,以下是定义命令行参数的方式。%0是所调用的程序名称。%1是第一个命令行参数。%2是第二个命令行参数。等到%9。让无涯教程看......
  • el-table fixed 之后 遮挡滚动条 或者 左浮动之后 遮挡合计列 滚动条不能拖动
    1.第一步讲浮动的table事件穿透.el-table__fixed,.el-table__fixed-right{ pointer-events:none;}/*表格固定列-鼠标事件穿透*/2.第二步 操作列浮动后按钮可点击.el-table__fixedtd,.el-table__fixed-righttd,.el-table__fixedth,.el-table__fixed-......
  • ubuntu设置中文输入法
    1、设置->区域与语言->管理已安装的语言,选择汉语,然后应用到整个系统,然后关闭。2、终端输入ibus-setup,弹出ibus设置窗口,在inputMehtod里选择Intelligentpinyin,然后add,close。3、在设置->区域与语言->输入源中选择汉语->intelligentpinyin->add.之后就可以用汉语输入法了,可......
  • 【misc】[HNCTF 2022 Week1]python2 input(JAIL) --沙盒逃逸,python2环境
    查看附件,这次有点不太一样,这次是python2的环境只有一个input函数,但是python2的input函数可是不太一样:在python2中,input函数从标准输入接收输入,并且自动eval求值,返回求出来的值在python2中,raw_input函数从标准输入接收输入,并返回输入字符串在python3中,input函数从标准输入接收输......
  • 配置Maven项目的pom.xml时遇到input contained no data问题
    1.问题2.解决我发现每次当我修改一些部分之后,就会遇到这个问题我遵循了他的建议重新排列标记属性,然后重新加载就解决了报错个人怀疑是修改后并没有识别之类的?......
  • 11月7日form表单与input框以及select标签
    目录form表单与input框form表单input标签input其它属性说明:form与其它标签以及input的应用再来展示一下禁用以及隐藏的属性select标签form表单与input框form表单功能:表单用于向服务器传输数据,从而实现用户与web服务器的交互表单能够包含input系列标签,比如文本字段、复选框、......
  • 输入法图标(语言栏)不见了怎么办
    1:右键点击任务栏,工具栏,勾选语言栏。如果没有,操作下边。2:双击我的电脑,控制面板,语言和区域选项,语言,详细信息,语言栏,勾选在桌面显示语言栏,确定。如果没有,操作下边。3:双击我的电脑,控制面板,语言和区域选项,语言,详细信息,已安装的服务(取消高级服务前的勾),点击键盘图标,添加,简体中文/美式键盘......