简体   繁体   中英

Phonegap Input focus on Mobile Safari

I am using the following code to scroll the window to position (0, 0) every time a user focuses on an input field within my Phonegap application:

<input type="text" id="answer" class="answer" />
...

$(document).on("touchstart", ".answer", function() {
  window.scrollTo(0, 0);
});

I also tried a setTimeout() , however this doesn't work either:

$(document).on("touchstart", ".answer", function() {
  window.scrollTo(0, 0);
  setTimeout(function() {
     scrollWindow();
  }, 10);

  var scrollWindow = function() {
     window.scrollTo(0, 0);
  }
});

The problem is that the window only scrolls the second time I tap the input field. I would like the window to scroll immediately when I tap the input field for the first time.

The reason why I am moving the window is because iOS spawns the native keyboard on focus of an input field and shifts the entire view up.

To do this I usually use "focus" instead of "touchstart".

$('#someFormId input').on("focus", function(event) {
    window.scrollTo(0, 0);
    document.body.scrollTop = 0;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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