position: fixed doesn't work on iPad and iPhone

后端 未结 15 1881
一整个雨季
一整个雨季 2020-11-22 16:16

I have been struggling with fixed positioning in iPad for a while. I know iScroll and it does not always seem to work (even in their demo). I also know that Sencha has a fix

相关标签:
15条回答
  • 2020-11-22 16:26

    Even though the CSS attribute {position:fixed;} seems (mostly) working on newer iOS devices, it is possible to have the device quirk and fallback to {position:relative;} on occasion and without cause or reason. Usually clearing the cache will help, until something happens and the quirk happens again.

    Specifically, from Apple itself Preparing Your Web Content for iPad:

    Safari on iPad and Safari on iPhone do not have resizable windows. In Safari on iPhone and iPad, the window size is set to the size of the screen (minus Safari user interface controls), and cannot be changed by the user. To move around a webpage, the user changes the zoom level and position of the viewport as they double tap or pinch to zoom in or out, or by touching and dragging to pan the page. As a user changes the zoom level and position of the viewport they are doing so within a viewable content area of fixed size (that is, the window). This means that webpage elements that have their position "fixed" to the viewport can end up outside the viewable content area, offscreen.

    What is ironic, Android devices do not seem to have this issue. Also it is entirely possible to use {position:absolute;} when in reference to the body tag and not have any issues.

    I found the root cause of this quirk; that it is the scroll event not playing nice when used in conjunction with the HTML or BODY tag. Sometimes it does not like to fire the event, or you will have to wait until the scroll swing event is finished to receive the event. Specifically, the viewport is re-drawn at the end of this event and fixed elements can be re-positioned somewhere else in the viewport.

    So this is what I do: (avoid using the viewport, and stick with the DOM!)

    <html>
      <style>
        .fixed{
          position:fixed;
          /*you can set your other static attributes here too*/
          /*like height and width, margin, etc.*/
          }
        .scrollableDiv{
          position:relative;
          overflow-y:scroll;
          /*all children will scroll within this like the body normally would.*/
          } 
        .viewportSizedBody{
          position:relative;
          overflow:hidden;
          /*this will prevent the body page itself from scrolling.*/
          } 
      </style>
      <body class="viewportSizedBody">
        <div id="myFixedContainer" class="fixed">
           This part is fixed.
        </div>
        <div id="myScrollableBody" class="scrollableDiv">
           This part is scrollable.
        </div>
      </body>
      <script type="text/javascript" src="{your path to jquery}/jquery-1.7.2.min.js"></script>
      <script>
        var theViewportHeight=$(window).height();
        $('.viewportSizedBody').css('height',theViewportHeight);
        $('#myScrollableBody').css('height',theViewportHeight);
      </script>
    </html>
    

    In essence this will cause the BODY to be the size of the viewport and non-scrollable. The scrollable DIV nested inside will scroll as the BODY normally would (minus the swing effect, so the scrolling does stop on touchend.) The fixed DIV stays fixed without interference.

    As a side note, a high z-index value on the fixed DIV is important to keep the scrollable DIV appear to be behind it. I normally add in window resize and scroll events also for cross-browser and alternate screen resolution compatibility.

    If all else fails, the above code will also work with both the fixed and scrollable DIVs set to {position:absolute;}.

    0 讨论(0)
  • 2020-11-22 16:30

    The simple way to fix this problem just types transform property for your element. and it will be fixed. Happy Coding :-)

    .classname{
      position: fixed;
      transform: translate3d(0,0,0);
    }
    

    Also you can try his way as well this is also work fine.

    .classname{
          position: -webkit-sticky;
        }
    
    0 讨论(0)
  • 2020-11-22 16:31

    position: fixed does work on android/iphone for vertical scrolling. But you need to make sure your meta tags are fully set. e.g

    <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
    

    Also if you're planning on having the same page work on android pre 4.0, you need to set the top position also, or a small margin will be added for some reason.

    0 讨论(0)
  • 2020-11-22 16:32

    now apple support that

    overflow:hidden;
    -webkit-overflow-scrolling:touch;
    
    0 讨论(0)
  • 2020-11-22 16:33

    I ended up using the new jQuery Mobile v1.1: http://jquerymobile.com/blog/2012/04/13/announcing-jquery-mobile-1-1-0/

    We now have a solid re-write that provides true fixed toolbars on the a lot of popular platforms and safely falls back to static toolbar positioning in other browsers.

    The coolest part about this approach is that, unlike JS-based solutions that impose the unnatural scrolling physics across all platforms, our scrolling feels 100% native because it is. This means that scrolling feels right everywhere and works with touch, mousewheel and keyboard user input. As a bonus, our CSS-based solution is super lightweight and doesn’t impact compatibility or accessibility.

    0 讨论(0)
  • 2020-11-22 16:39

    I had this problem on Safari (iOS 10.3.3) - the browser was not redrawing until the touchend event fired. Fixed elements did not appear or were cut off.

    The trick for me was adding transform: translate3d(0,0,0); to my fixed position element.

    .fixed-position-on-mobile {
      position: fixed;
      transform: translate3d(0,0,0);
    }
    

    EDIT - I now know why the transform fixes the issue: hardware-acceleration. Adding the 3D transformation triggers the GPU acceleration making for a smooth transition. For more on hardware-acceleration checkout this article: http://blog.teamtreehouse.com/increase-your-sites-performance-with-hardware-accelerated-css.

    0 讨论(0)
提交回复
热议问题