Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll textfield up when keyboard popsup

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.

like image 783
user533844 Avatar asked Aug 26 '13 18:08

user533844


2 Answers

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 example

like image 163
MikeM Avatar answered Sep 30 '22 07:09

MikeM


To complete the answer, if you want to animate the scroll replace:

$htmlOrBody.scrollTop(textareaTop - scrollTopPadding);

by

var timing = 250;
$htmlOrBody.animate({ scrollTop: textareaTop - scrollTopPadding }, timing);
like image 35
Ludo Avatar answered Sep 30 '22 08:09

Ludo