Unwanted auto-scrolling to top on input focus, with Safari

落花浮王杯 提交于 2021-02-04 15:18:30

问题


I have this page:

    <html>

      <body>
        <div class="app">
          <div class="main-panel">
            <nav class="navbar"></nav>
            <div class="sidenav">
              <ul>
                <li>First</li>
                <li>2nd</li>
                <li>3rd</li>
                <li>4th</li>
              </ul>
            </div>
            <div class="page-container">
              <div class="placeholder">
                <ul>
                  <li>First</li>
                  <li>2nd</li>
                  <li>3rd</li>
                  <li>4th</li>
                </ul>
              </div>
              <input type="text">
            </div>
          </div>
        </div>
      </body>

    </html>

And the following css:

    html, body {
      height: 100%;
      width: 100%;
      position: fixed;
      overflow: visible; }

    .app {
      background-color: #f4f5f8;
      height: 100%;
      overflow-x: hidden;
      overflow-y: auto; }

    .main-panel {
      height: 100%;
      display: grid;
      grid-template-columns: 210px 1fr;
      grid-template-rows: 76px 1fr; }

    .navbar {
      grid-column: 1 / 3; }

    .page-container {
      overflow-y: auto;
      overflow-x: hidden; }

    .placeholder {
      display: block;
      height: 500px;
      width: 100%; }

Also available at: https://jsfiddle.net/1vk5jcuL/

On Safari 12.1 (MacOS and iOS), if you scroll down the main body and hover/focus over the input, it will automatically scroll to top.

I would like to keep the scrolling behavior the same (i.e. when you scroll you scroll only the main panel, not the side-nav, but safari shouldn't scroll to top on input focus.

This problem is purely about HTML + CSS, so no JS involved.

Edit: this is a GIF recording of it happening: https://imgur.com/a/hPD9YuP


回答1:


Replace 1fr with minmax(0, 1fr) @helios. it worked for me. Here is the full explanation of it. https://css-tricks.com/preventing-a-grid-blowout/. I tested with your fiddle. it worked fine.




回答2:


set a max-height:100% on your page-container

.page-container
  overflow-y: auto
  overflow-x: hidden
  max-height: 100%


来源:https://stackoverflow.com/questions/55816110/unwanted-auto-scrolling-to-top-on-input-focus-with-safari

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!