Mobile safari position:fixed z-index glitch when scrolling

后端 未结 4 727
礼貌的吻别
礼貌的吻别 2020-12-13 00:22

I know iPhones used to not support position:fixed, but now it does and I\'m seeing a weird glitch when I scroll a fixed position element behind other elements with

相关标签:
4条回答
  • 2020-12-13 00:53

    I tried the solution accepted as an answer in a specific case when I needed to set different position in the stack of different layers, but that alone didn't work both in desktop browsers (firefox and chrome) and Safari iOS

    I came out with this hack which uses both translateZ and z-index for each div, which is working in those platforms. The order of translateZ and z-index is important. For setting each layers position is

    -webkit-transform:translateZ(1px);
    -moz-transform:translateZ(1px);
    -o-transform:translateZ(1px);
    transform:translateZ(1px);
    position: relative; 
    z-index: 1; 
    

    I used the same value for the z-index and translateZ just for consistency, it is not necessary. See working example http://jsbin.com/peyehufo/5

    0 讨论(0)
  • 2020-12-13 00:56

    Update 1: I added transform:translateZ(x) in addition to the z-index and it did not fix the problem.

    Update 2: I added -webkit- prefix and this DOES fix the z-index problem on mobile Safari, but also causes the position:fixed to work incorrectly in desktop Chrome. "

    Then try to wrap -webkit-transform:translateZ(x) in a mobile specific media query.
    For example:

    @media only screen and (min-device-width : ... ) and (max-device-width : ... ) {
        .whatever {
            -webkit-transform: translateZ(x)
        }
    }
    

    So in this case it won't do anything on desktop Chrome

    0 讨论(0)
  • 2020-12-13 00:58

    z-index is not reliable with position:fixed, as shown in this fiddle: http://jsfiddle.net/mZMkE/2/ use translateZ transformation instead.

    transform:translateZ(1px);
    

    on your page elements.

    EDIT: In your code, Add this css:

    .bla, .projects, .contact  {
          -webkit-transform:translateZ(1px);
          -moz-transform:translateZ(1px);
          -o-transform:translateZ(1px);
          transform:translateZ(1px);
    }
    

    and then remove z-index refs from those elements and .intro.

    0 讨论(0)
  • 2020-12-13 01:16

    I'm not advocating for this solution, but it's the best I've got at the moment...

    In order to replicate the effect of z-index with position fixed on an iPhone, it seems to require two techniques together:

    1. As suggested by @tnt above, use transform:translateZ(n) where z-index is used to get mobile safari to handle the stack order correctly. This appears to have the unfortunate side-effect of causing the position:fixed to stop working...

    2. Instead of position:fixed, use a javascript technique like this to fake it.

    I haven't tested this thoroughly (because I'm not going to do it), but it seems to work fairly well. Although the "fixed" element seems to stutter a bit.

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