如何做一个听话的 “输入框”

前言

在移动端的web开发中,一提起输入框,程序猿(媛)肯定有很多可以吐槽的点。

在输入框的运用中,小编也是很心累呀~

不过,经过我 潜(cai)心(keng)研(jiao)究(xun),也算是了解了它的脾性... ...  

特别鸣谢:周涵,家兴等

正文这里开始 ? — — — — — — — —

问题探究

1. ios中,输入框获得焦点时,页面输入框被遮盖,定位的元素位置错乱:

当页input存在于吸顶或者吸底元素中时,用户点击输入框,输入法弹出后,fiexd失效,页面中定位好的元素随屏幕滚动。

针对这个问题,我们一起来看下以下几种方案:

方案一: Web API 接口 :scrollIntoView 的应用,将input输入框显示在可视区域。

  
  
  1. // 输入框获得焦点时,元素移动到可视区域

  2. inputOnFocus(e) {

  3.    setTimeout(function(){

  4.        e.target.scrollIntoView(true);

  5.        // true:元素的顶端将和其所在滚动区的可视区域的顶端对齐; false:底端对齐。

  6.    },200);  // 延时 == 键盘弹起需要时间

  7. }

一行代码,轻松搞定,输入框就乖乖的出现在你眼前了。

不过有点小缺陷:页面过长时,由于fixed失效,输入框依然也会跟着页面滑走。

这时,我们需要一个固定的输入框......

方案二:在输入框获得焦点时,将页面滑动到最底部,避免fixed导致的页面乱飞,并且保证input在最底部。

  
  
  1. var timer

  2. // 输入框获得焦点时,将元素设置为position:static,设置timer

  3. inputOnFocus(e) {

  4.    e.target.style.className = 'input input-static';

  5.    timer = setInterval(

  6.        function() {

  7.            document.body.scrollTop = document.body.scrollHeight

  8.        }, 100)

  9. };

  10. // 输入框失去焦点时,将元素设置为 position:fixed,清除timer

  11. inputOnbulr(e) {

  12.    e.target.parentNode.className = 'input input-fixed';

  13.    clearInterval(timer)

  14. };

效果如下图

当获得焦点弹出虚拟键盘后,input输入框会一直紧贴键盘顶部。如果,你的页面弹出输入法后不需要滑动查看其他内容,那么你对这种方案应该很中意。

But,可能你做的是一个类似聊天的页面,需要在回复时,查看历史消息,那么,请你继续往下看

方案三:将页面进行拆分: 页面(main) = 内容(sectionA) + 输入框(sectionB)+ 其他(sectionOther)

原理 : main.height = window.screen.height ;
sectionA 绝对定位,进行内部滚动 overflow-y:scroll ;
sectionB 可保证在页面最底部。

  
  
  1. .main { position: relative; height: 100%; }

  2. .sectionA { box-sizing: border-box; padding-bottom: 60px; height: 100%; overflow-y: scroll; -webkit-overflow-scrolling: touch //为了使滚动流畅,sectionA 添加属性 }

  3. .sectionB { position: absolute; height: 60px; overflow: hidden; left: 0; right: 0; bottom: 0; }

纯css3打造,可以滚动,可以固定位置,基本满足大部分布局需要。

2. IOS 中单行输入框输入内容长被遮盖,不能显示全部,且不能左右滑动。

class="video_iframe" data-vidtype="2" allowfullscreen="" frameborder="0" data-ratio="0.5666666666666667" data-w="272" data-src="http://v.qq.com/iframe/player.html?vid=h13325j1rnn&width=670&height=502.5&auto=0" style="display: block; width: 670px !important; height: 502.5px !important;" width="670" height="502.5" data-vh="502.5" data-vw="670" src="http://v.qq.com/iframe/player.html?vid=h13325j1rnn&width=670&height=502.5&auto=0"/>

这个是IOS的一个bug,可以考虑用 textarea 替换 input,设置一行的高,进行上下滚动查看。(其他方案可以参看下面 第 6 点)

3. 获得焦点时,光标消失或错位:

class="video_iframe" data-vidtype="2" allowfullscreen="" frameborder="0" data-ratio="0.5666666666666667" data-w="272" data-src="http://v.qq.com/iframe/player.html?vid=x1332b4cdsa&width=670&height=502.5&auto=0" style="display: block; width: 670px !important; height: 502.5px !important;" width="670" height="502.5" data-vh="502.5" data-vw="670" src="http://v.qq.com/iframe/player.html?vid=x1332b4cdsa&width=670&height=502.5&auto=0"/>


 -webkit-user-select:none 导致 input 框在 iOS 中无法输入,光标不出现,设置如下

  
  
  1. user-select:text;

  2. -webkit-user-select:text;

利用scrollIntoView 使当前元素出现到指定位置,避免光标错位,设置如下:

  
  
  1. e.target.scrollIntoView(true);  

  2. e.target.scrollIntoViewIfNeeded();

4. 进入页面如何自动获取焦点,弹出软键盘?
  • 添加 autofocus 属性 支持自动获得焦点

  • 触发 focus() 事件

5.随文字输入,输入框宽度自适应。
  
  
  1.  onkeyPress(e) {

  2.    const testLength = e.target.value.length;

  3.    e.target.style.width = `${testLength*8+10}px`

  4.  }

这种方案基本满足自动获取效果。

testLength * 8 英文字符,testLength * 16中文字符, +10为后边光标预留位置。 这种方案显然不适用于对精确度有很高要求的需求。

6. 介绍一个属性:contenteditable,模拟输入时动态获取宽高

(1)div设置contentditable=true 可以将此元素变成可输入状态。

  
  
  1. <div  class="inputContent"  contenteditable="true" ></div>

(2)想要变成input输入框,利用css模拟输入框的样式

  
  
  1. .inputContent{

  2.  color:#444;

  3.  border:#999 solid 1px;

  4.  border-radius: 3px;

  5.  padding: 5px 10px;

  6.  box-sizing: border-box;

  7.  min-width:50px;

  8.  max-width: 300px;

  9.  background: #ffffff;

  10. }

这里配合min-width,max-width 效果更真实。

(3)点击div可以弹出软键盘,但是无法输入内容,需要设置属性,如下

  
  
  1. .inputContent{

  2.  user-select:text;

  3.  -webkit-user-select:text;

  4. }

这样就完成一个可以根据获取输入内容来动态来调节宽高。

(这里是一个gif图)

还可以利用js模拟placeholder等,这里就不展开了

7.其他问题及解决
  • 输入框获得焦点可弹出软键盘,却没有光标闪烁,也无法正常输入。

    -webkit-user-select:none 导致的,可以这样解决


  
  
  1. *:not(input,textarea) {

  2.    -webkit-touch-callout: none;

  3.    -webkit-user-select: none;

  4. }


  • input 自定义样式

  
  
  1. // 使用伪类

  2. input::-webkit-input-placeholder,

  3. input::-moz-placeholder,

  4. input::-ms-input-placeholder {

  5.  ...style

  6.  text-align: center;

  7. }


好了,就写到这了,希望看过后对你能有帮助。

输入框中的坑还是很多了,本文也难免会有有含盖不全的情况,欢迎小伙伴们给我们留言,共同探讨。

持续关注我们,会给大家奉献更多精品文章。

——————————————————

长按二维码,关注大转转FE


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值