Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save PhilippSoehnlein/1336369 to your computer and use it in GitHub Desktop.
Save PhilippSoehnlein/1336369 to your computer and use it in GitHub Desktop.
Modernizr Test for position fixed (including iOS tests)
/* modernizr-test_position_fixed_ios.js
* Original by Daniel Ott (https://gist.github.com/1333800)
* 3 March 2011
* Updated by Philipp Söhnlein 3 November 2011
* Custom Tests using Modernizr's addTest API
*/
/* iOS
* There may be times when we need a quick way to reference whether iOS is in play or not.
* While a primative means, will be helpful for that.
*/
Modernizr.addTest('ipad', function () {
return !!navigator.userAgent.match(/iPad/i);
});
Modernizr.addTest('iphone', function () {
return !!navigator.userAgent.match(/iPhone/i);
});
Modernizr.addTest('ipod', function () {
return !!navigator.userAgent.match(/iPod/i);
});
Modernizr.addTest('appleios', function () {
return (Modernizr.ipad || Modernizr.ipod || Modernizr.iphone);
});
/* CSS position:fixed
* Not supported in older IE browsers, nor on Apple's iOS devices.
* Actually the token example on the Modernizr docs. http://www.modernizr.com/docs/
*/
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);
}
/* Uh-oh. iOS < 5 would return a false positive here.
* If it's about to return true, we'll explicitly test for known iOS User Agent strings.
* "UA Sniffing is bad practice" you say. Agreeable, but sadly this feature has made it to
* Modernizr's list of undectables, so we're reduced to having to use this. */
navigator.userAgent.match(/OS (\d)/i);
return ret && (!Modernizr.appleios || (Modernizr.appleios && RegExp.$1 >= 5));
});
@twestonkendall
Copy link

thanks so much! it works great. spent 5 hours trying to get anything to work.

@jimryan
Copy link

jimryan commented Jan 19, 2012

Fantastic, thank you!

@wheresrhys
Copy link

Not sure how (or if it's possible even) to do a pull request on gist, but I've made this version https://gist.github.com/wheresrhys/5160401 which is more efficient on ipad as it does the user agent test first.

PS - cheers for sharing the test - very useful

@heaversm
Copy link

This is great! One problem is that, like iOS <5, Android 2.3.2 and below have the same issue. How would I also detect for these browsers?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment