21

Modernizr is great but the example test for position: fixed is quite incomplete:

  • iOS 4 and lower returns true while it doesn't support position: fixed
  • Opera on Windows returns false while it does support position: fixed

I found another test based on the Modernizr test but with iOS detection added: https://gist.github.com/855078/109ded4b4dab65048a1e7b4f4bd94c93cebb26b8. It isn't really future proof since the upcoming iOS 5 does support position: fixed.

Can you guys help me find a way to test position fixed in iOS without browser sniffing?

// Test for position:fixed support
Modernizr.addTest('positionfixed', function () {
    var test  = document.createElement('div'),
      control = test.cloneNode(false),
         fake = false,
         root = document.body || (function () {
            fake = true;
            return document.documentElement.appendChild(document.createElement('body'));
      }());

   var oldCssText = root.style.cssText;
   root.style.cssText = 'padding:0;margin:0';
   test.style.cssText = 'position:fixed;top:42px';
   root.appendChild(test);
   root.appendChild(control);

   var ret = test.offsetTop !== control.offsetTop;

   root.removeChild(test);
   root.removeChild(control);
   root.style.cssText = oldCssText;

   if (fake) {
      document.documentElement.removeChild(root);
   }

   return ret;
});
Volker E.
  • 5,443
  • 11
  • 43
  • 62
DADU
  • 5,236
  • 5
  • 38
  • 61
  • Hi, are you using the latest version of Modernizer. A new one was released today or yesterday I think and it covered such bugs. In anycase, position:fixed is poor on current mobile browsers. – Francisc Jun 11 '11 at 21:22
  • Yep, using Modernizr 2. It hasn't changed that example in the documentation though. – DADU Jun 11 '11 at 21:30
  • I meant like 2.X. If you downloaded the library in the last 2-3 days tops, it's probably either a Modernizer bug or one in your CSS where you mention set the element to be fixed (i.e. position:fixd; or positio:fixed, spelling). – Francisc Jun 11 '11 at 21:39
  • Using 2.0.4 (currently latest). It's no CSS typo since position fixed does its work in browsers that support it. – DADU Jun 11 '11 at 22:11
  • 3
    I don't have a solution for you, but the modernizr folk are aware of this issue: https://github.com/Modernizr/Modernizr/issues/167 -- might be worth your while shaking their tree to see if they can get it fixed. – Spudley Jul 01 '11 at 10:24
  • 12
    It's one of the great unsolved feature detection mysteries of the moment. So far there is no working feature detect for fixed pos that handles mobile webkit successfully. – Paul Irish Jul 19 '11 at 18:20
  • Hi Paul, thanks for joining in! Curious if the future will feature a feature detect. – DADU Jul 19 '11 at 19:24
  • 1
    My understanding is that support for postiton:fixed requires scrolling. Mobile safari, to date, doesn't scroll. Instead, you move and resize a viewport which hovers above the content. Would it be enough to test for scrollability? Or does IOS fail that test too? – jimbo Jul 25 '11 at 20:20
  • Has the situation evolved since last July? Is there a working feature detect for position:fixed now? – julien_c Feb 22 '12 at 19:54
  • 1
    @julien_c While there are many new tests, Modernizr 2.5 still doesn't ship one for position:fixed. – DADU Feb 22 '12 at 21:59
  • see it : http://stackoverflow.com/questions/9038625/detect-if-device-is-ios/12953515#12953515 – sgt9dg8s90 Oct 18 '12 at 11:42

2 Answers2

1

I wrote this test for iOS: http://mnobeta.no/2011/09/test-position-fixed-for-iphone/

It is a bit messy, but seems to work. Android is still a problem because of its "fake" position:fixed.

Verbeia
  • 4,370
  • 2
  • 20
  • 44
0

I have found that you need to insert some hacks to get a functional positionFixed test. For example i have inserted a hack into my test that returns true for iOS deviced running v.5 or above:

/*iPhone/iPad Hack*/
if(navigator.userAgent.match(/iPad|iPhone/i) !== null){
    /*Check if device runs iOS 5 or higher*/
    isSupported = navigator.userAgent.match(/[5-9]_[0-9]/) !== null;
}

I'm not sure how "clean" this code is, but it does the trick for me.

Fjandin
  • 79
  • 4