Scroll textfield up when keyboard popsup

前端 未结 3 1502
旧时难觅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

    just detect browser window size change, when keyboard pops up, the browser window size will change

    $(window).resize(function() {
        var $htmlOrBody = $('html, body'), // scrollTop works on  for some browsers,  for others
        scrollTopPadding = 8;
        // get input tag's offset top position
        var textareaTop = $(this).offset().top;
        // scroll to the textarea
        $htmlOrBody.scrollTop(textareaTop - scrollTopPadding);
    
        // OR  To add animation for smooth scrolling, use this. 
        //$htmlOrBody.animate({ scrollTop: textareaTop - scrollTopPadding }, 200);
    });
    

提交回复
热议问题