简体   繁体   English

在移动 Safari/iPad/iOS 8 中防止身体滚动到顶部

[英]Preventing body scrolling to top in mobile Safari/iPad/iOS 8

I am using the bootstrap affix component to give a fixed nav bar at the top of the page.我正在使用 bootstrap 词缀组件在页面顶部提供一个固定的导航栏。 This nav bar contains form elements.此导航栏包含表单元素。 When a form element is clicked on (given focus) the body of the page scrolls to the top.当一个表单元素被点击(给定焦点)时,页面的主体会滚动到顶部。 Is there a way to override this behaviour to prevent the scrolling?有没有办法覆盖此行为以防止滚动?

To save someone else 4 hours+ trawling for answers I'll attempt to answer my own question. 为了节省别人4个多小时的拖网时间,我将尝试回答我自己的问题。

There doesn't seem to be a solution for using form elements on a fixed div without jumping. 在固定的div上不跳过而使用表单元素似乎没有解决方案。

My solution was to change the markup to a bootstrap-style drop-down. 我的解决方案是将标记更改为引导程序样式的下拉菜单。

Original select form element markup: 原始选择表单元素标记:

<select id="gender" name="gender" class="fake-select">
  <option value="Male">Male</option>
  <option value="Female">Female</option>
</select>

Bootstrap style drop-down markup: Bootstrap样式下拉标记:

   <li class="dropdown open">
     <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="true">Dropdown <span class="caret"></span>/a>
     <ul class="dropdown-menu" role="menu">
       <li><a href="#">Male</a></li>
      <li><a href="#">Female</a></li>
    </ul>
  </li>

Created a plunker to demonstrate this: http://plnkr.co/hYTJ9l 创建了一个插件来证明这一点: http ://plnkr.co/hYTJ9l

https://github.com/takien/FakeSelect library that offers a quick solution by manipulating the rendered markup. https://github.com/takien/FakeSelect库,该库通过处理渲染的标记提供了快速的解决方案。

I have my form in a modal popup. 我在模式弹出窗口中有表单。 The .modal-open class is added to the body when the modal shows. 模态显示时,.modal-open类将添加到主体。 I added the following style to the body to prevent it from scrolling while the popup is active. 我在主体上添加了以下样式,以防止其在弹出窗口处于活动状态时滚动。

.modal-open {
  position: fixed;
}

将 Safari 浏览器更新至 14 或更高版本

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

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