How to make fixed-content go above iOS keyboard? How to make fixed-content go above iOS keyboard? google-chrome google-chrome

How to make fixed-content go above iOS keyboard?


Mobile Safari does not support position: fixed when an input focused and virtual keyboard displayed.

To force it work the same way as Mobile Chrome, you have to use position: absolute, height: 100% for the whole page or a container for your pseudo-fixed elements, intercept scroll, touchend, focus, and blur events.

The trick is to put the tapped input control to the bottom of screen before it activates focus. In that case iOS Safari always scrolls viewport predictably and window.innerHeight becomes exactly visible height.

Open https://avesus.github.io/docs/ios-keep-fixed-on-input-focus.html in Mobile Safari to see how it works.

Please avoid forms where you have several focusable elements because more tricks to fix position will be necessary, those were added just for demonstration purposes.

Note that for rotation and landscape mode, additional tricks are necessary. I'm working on a framework called Tuff.js which will provide a full-screen container helping mobile web developers to build web applications much faster. I've spend almost a year on the research.

By the way, to prevent scrolling of the whole window when virtual keyboard is active, you can use this super simple trick

var hack = document.getElementById('scroll-hack');function addScrollPixel() {  if (hack.scrollTop === 0) {    // element is at the top of its scroll position, so scroll 1 pixel down    hack.scrollTop = 1;  }  if (hack.scrollHeight - hack.scrollTop === hack.clientHeight) {    // element is at the bottom of its scroll position, so scroll 1 pixel up    hack.scrollTop -= 1;  }}if (window.addEventListener) {  // Avoid just launching a function on every scroll event as it could affect performance.   // You should add a "debounce" to limit how many times the function is fired  hack.addEventListener('scroll', addScrollPixel, true);} else if (window.attachEvent) {  hack.attachEvent('scroll', addScrollPixel);}
body {  margin: 0 auto;  padding: 10px;  max-width: 800px;}h1>small {  font-size: 50%;}.container {  display: flex;  align-items: top;  justify-content: space-between;}.container>div {  border: #000 1px solid;  height: 200px;  overflow: auto;  width: 48%;  -webkit-overflow-scrolling: touch;}
<h1>iOS Scroll Hack</h1><p>Elements with overflow:scroll have a slightly irritating behaviour on iOS, where when the contents of the element are scrolled to the top or bottom and another scroll is attempted, the browser window is scrolled instead. I hacked up a fix using minimal,  native JavaScript.</p><p>Both lists have standard scrolling CSS applied (<code>overflow: auto; -webkit-overflow-scrolling: touch;</code>), but the list on the right has the hack applied. You'll notice you can't trigger the browser to scroll whilst attempting to scroll the list  on the right.</p><p>The only very slight drawback to this is the slight "jump" that occurs when at the top or bottom of the list in the hack.</p><div class='container'>  <div id='scroll-orig'>    <ul>      <li>1</li>      <li>2</li>      <li>3</li>      <li>4</li>      <li>5</li>      <li>6</li>      <li>7</li>      <li>8</li>      <li>9</li>      <li>10</li>      <li>11</li>      <li>12</li>      <li>13</li>      <li>14</li>      <li>15</li>      <li>16</li>      <li>17</li>      <li>18</li>      <li>19</li>      <li>20</li>    </ul>  </div>  <div id='scroll-hack'>    <ul>      <li>1</li>      <li>2</li>      <li>3</li>      <li>4</li>      <li>5</li>      <li>6</li>      <li>7</li>      <li>8</li>      <li>9</li>      <li>10</li>      <li>11</li>      <li>12</li>      <li>13</li>      <li>14</li>      <li>15</li>      <li>16</li>      <li>17</li>      <li>18</li>      <li>19</li>      <li>20</li>    </ul>  </div></div>