键盘弹出时向上滚动文本字段 [英] Scroll textfield up when keyboard popsup

查看:127
本文介绍了键盘弹出时向上滚动文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用html5 / javascript / jQuery / css进行移动应用开发。我在应用程序中有多个textareas。当我点击它输入时,键盘弹出(android选项卡)。但是textarea仍然保留在该页面上的位置。如何在键盘弹出时滚动页面。

I am using html5/javascript/jQuery/css for mobile app development. I have multiple textareas in the app. When I click on that to input, keyboard popup (android tab). But the textarea stays where it's on that page. How can I scroll page when keyboard pops up.

推荐答案

使用jQuery,获取textarea的 offset()。top 值然后使用 scrollTop()

with jQuery, get the textarea's offset().top value then set document scroll position using scrollTop()

var $htmlOrBody = $('html, body'), // scrollTop works on <body> for some browsers, <html> for others
    scrollTopPadding = 8;

$('textarea').focus(function() {
    // get textarea's offset top position
    var textareaTop = $(this).offset().top;
    // scroll to the textarea
    $htmlOrBody.scrollTop(textareaTop - scrollTopPadding);
});

jsfiddle示例

这篇关于键盘弹出时向上滚动文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆