简体   繁体   English

Android浏览器中的Textarea由键盘隐藏

[英]Textarea in Android browsers hidden by keyboard

In iOS, even for textareas contained in a div with position: fixed , when a textarea has focus, the OS ensures that it is visible (which sometimes means sliding the entire browser window up) so that the textarea isn't hidden by the keyboard. 在iOS中,即使对于包含在position: fixed的div中的textareas,当textarea具有焦点时,操作系统也会确保它是可见的(这有时意味着向上滑动整个浏览器窗口),以便键盘不会隐藏textarea 。

In Android browsers (I've tested the stock browser in both 2.3 and 4.0 as well as Chrome in 4.0), this does not happen. 在Android浏览器中(我已经在2.3和4.0以及4.0中的Chrome中测试过股票浏览器),但这不会发生。 The textarea gets covered by the keyboard, and the user can't see what she's typing. textarea被键盘覆盖,用户无法看到她正在键入的内容。

As a temporary workaround for Android only, I set position: fixed on textarea:focus and move it to the top of the screen so that it's guaranteed to be visible. 作为Android的临时解决方法,我设置position: fixedtextarea:focus并将其移动到屏幕顶部,以确保它是可见的。

Are there any more elegant solutions that maintain the integrity of my layout? 有没有更优雅的解决方案来保持我的布局的完整性?

I made a little example in jsfiddle. 我在jsfiddle中做了一个小例子。 View in an Android browser to see what I mean: http://fiddle.jshell.net/5cvj5/show/light/ 在Android浏览器中查看我的意思: http//fiddle.jshell.net/5cvj5/show/light/

What I ended up doing, and what I found that Facebook does to overcome the same challenge, was to add a spacer block element below the textarea when it receives focus and then to scroll to the bottom of the view, like so: 我最终做了什么,以及我发现Facebook为克服同样的挑战所做的是在获得焦点时在textarea下面添加一个spacer块元素,然后滚动到视图的底部,如下所示:

var android_spacer = $('<div/>', {
    'class' : 'android_spacer'
}).css({
    'width'  : '100%',
    'height' :  '200px'
}).appendTo($('#parent_view'));

$('#the_textarea').on('focus', function() {
    $(this).after(android_spacer);
});

$('#the_textarea').on('blur', function() {
    $('.android_spacer').remove();
});

Note that there's only ever one spacer onscreen at a time. 请注意,屏幕上一次只有一个间隔符。

只需将页脚的位置设置为绝对,并将底部设置为0,这将处理它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM