简体   繁体   English

滚动后无法聚焦输入?

[英]Can't focus input after scroll?

In mobile browser, when focus input text, I want this input scroll to top 在移动浏览器中,当焦点输入文本时,我希望此输入滚动到顶部

$(this.$el).find('input, textarea').on('focus', function(e) {
  setTimeout((function() {
    var pos = $(this).position();
    if (pos.top > window.innerHeight) {
      var scrollTopOffset = pos.top - window.innerHeight + this.offsetHeight + 10
      $scroller.scrollTop($scroller.scrollTop() + scrollTopOffset)
    }
  }).bind(this), 500); // After the keyboard up
  $(this).focus()
})

after the input scroll to the correct position, can't focus the input 输入滚动到正确位置后,无法聚焦输入

touch the input then focus is ok, after that the keyboard up then the input scroll to top, then loose the focus 触摸输入,然后确定焦点,然后向上移动键盘,然后将输入滚动到顶部,然后松开焦点

How to focus the input after scroll? 滚动后如何聚焦输入?

I have tried this, by emulating the browser for mobile and it works. 我已经通过模拟移动设备的浏览器进行了尝试,并且可以正常工作。

$('input, textarea').on('focus', function() {
    document.body.scrollTop = $(this).offset().top;
    $(this).focus();
});

Hope This will help you.. Thanks 希望对您有帮助。.谢谢

var cursorFocus = function(elem) {
      var x = window.scrollX, y = window.scrollY;
      elem.focus();
      window.scrollTo(x, y);
    }

    cursorFocus(document.getElementById('search-terms'));

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

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