简体   繁体   English

在移动Safari中显示键盘时,防止屏幕向上滚动

[英]Prevent screen scrolling up when keyboard is shown in mobile Safari

I'm building an Angularjs app that has a at the bottom of one view. 我正在构建一个Angularjs应用程序,它在一个视图的底部。

Problem in mobile safari on iOS9: When focusing the textarea the soft keyboard is shown and covers the lower part of the view. iOS9上移动游侠中的问题:当聚焦textarea时,显示软键盘并覆盖视图的下半部分。

How can I scroll the page up when the keyboard is visible so that the content (ie the textarea) is not covered? 当键盘可见时,如何向上滚动页面以便不覆盖内容(即textarea)?

Here is one way you might prevent scrolling. 这是阻止滚动的一种方法。 Add overflow:hidden to the document body when your inputs are in focus. 添加overflow:hidden当输入处于焦点时overflow:hidden到文档正文。

  function toArray (collection) { return Array.prototype.slice.call(collection); } function noScroll (event) { if (event.type === 'focus') { document.body.classList.add('no-scroll'); } else if (event.type === 'blur') { document.body.classList.remove('no-scroll'); } } var inputs = toArray(document.querySelectorAll('input')); inputs.forEach(function(input){ input.addEventListener('focus',noScroll,false); input.addEventListener('blur',noScroll,false); }); 
 .no-scroll { overflow:hidden; } 

To scroll the page up, you could use document.body.scrollTop when the inputs are in focus and set the value to the desired location. 要向上滚动页面,可以在输入处于焦点时使用document.body.scrollTop并将值设置为所需位置。

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

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