How to stop mobile safari from setting fixed positions to absolute on input focus?

陌路散爱 提交于 2019-12-30 08:29:23

问题


Disclaimer - I understand there exists questions around fixed elements in safari, and fixed elements weren't supported, but now are and so forth. However I can't find a question that addresses this exact question.

Given the simplest of fixed sidebars, something like:

.sidebar {
    position: fixed;
    top: 10px;
    right: 10px;
}

And a relatively long page, with input elements.

When an input element is focused, any fixed element becomes absolute - I understand the why, safari is trying to declutter the viewport - thats fine, but not always appropriate. I ask that I get to choose the best experience for the user (i know best naturally).

So the Question..

Is there any way to leave fixed elements as fixed even when input elements are focused?

I have attempted to do a bit of $(window).on('scroll', magic and position elements manually on scroll, but its quite jittery on the ipad.


回答1:


Safari has supported position: fixed since at least version 9.2, but if you're seeing difficult issues, you can fully create the fixed position effect by making the document element and body full screen and then using absolute positioning. Scrolling then occurs in some main container element rather than the body. Your "fixed" elements can exist anywhere in the markup using this method.

jsfiddle here

html,
body,
.mainContainer {
  height: 100%;
  width: 100%;
  overflow: hidden;
  margin: 0;
}

.mainContainer {
  overflow: auto;
}

.fixed {
  position: absolute;
  bottom: 20px;
  left: 20px;
}



回答2:


In order to achieve the effect you desire you need to change your approach to the layout. Instead of positioning the sidebar with position:fixed you need to use position:absolute within a position:relative container that is set to the height of the viewport within that position:relative container you need another div that uses overflow-y: scroll and -webkit-overflow-scrolling : touch

Caveat: I generally avoid using position fixed on tablet & mobile if possible although the browser support is there, in my experience it'll be janky and javascript solutions leave a lot to be desired, my first response would be to challenge the pattern with the designer. If I'm given designs that include a position fixed element when there are input elements, I'm more likely to seek a design solution than a development one as the focus issues you're describing are difficult to circumvent and maintain a quality user experience.

THE MARKUP:

<div class="outer">
   <div class="sidebar">
    <ul>
       <li>Dummy list nav or something</li>

     </ul>
  </div>
  <div class="container">
       <input type="text" />
  <!-- I added 10000 inputs here as a demo -->
 </div>
</div>

THE CSS:

html,body{
-webkit-overflow-scrolling : touch !important;
overflow: auto !important;
height: 100% !important;
}
.outer {
  position: relative;
  overflow: hidden;

/* I'm using Viewport Units here for ease, but I would more likely check the height of the viewport with javascript as it has better support*/
  height: 100vh;
}
.sidebar {
    position: absolute;
    top: 10px;
    right: 10px;
  /*added bg colour for demo */

  background: blue;
}
.container {
  height: 100vh;
  overflow-y: scroll;
  -webkit-overflow-scrolling: touch
}
input {
  display: block;
}

Here's a CodePen for you to open in your simulator (presentation view): https://codepen.io/NeilWkz/full/WxqqXj/

Here's the editor view for the code: https://codepen.io/NeilWkz/pen/WxqqXj



来源:https://stackoverflow.com/questions/38987153/how-to-stop-mobile-safari-from-setting-fixed-positions-to-absolute-on-input-focu

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