Scroll textfield up when keyboard popsup

前端 未结 3 1503
旧时难觅i
旧时难觅i 2021-02-04 14:21

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 tex

3条回答
  •  抹茶落季
    2021-02-04 15:09

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

    var $htmlOrBody = $('html, body'), // scrollTop works on  for some browsers,  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

提交回复
热议问题