Skip to content

Instantly share code, notes, and snippets.

@kathangeorg
Created March 21, 2012 09:00
Show Gist options
  • Save kathangeorg/2145704 to your computer and use it in GitHub Desktop.
Save kathangeorg/2145704 to your computer and use it in GitHub Desktop.
iOS, Android detection and position fixed
var ua = navigator.userAgent;
var checker = {
iphone: ua.match(/(iPhone|iPod|iPad)/),
blackberry: ua.match(/BlackBerry/),
android: ua.match(/Android/)
};
/*Check if device runs iOS 5 or higher*/
fiveorhigher = navigator.userAgent.match(/[5-9]_[0-9]/) !== null;
if(console) {console.log(fiveorhigher);}
//alert(navigator.userAgent);
/*Check if device runs Android 3 or higher*/
threeorhigher = navigator.userAgent.match(/[3-9].[0-9].[0-9]/) !== null;
if(console) {console.log(threeorhigher);}
//alert(navigator.userAgent);
// position fixed hack for ios < 5.0 and android < 3.0.0
window.onscroll = function() {
if( ((navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod') && !fiveorhigher) || ((checker.android) ) && !threeorhigher ) {
document.getElementById('containerFixed').style.top =
(window.pageYOffset + window.innerHeight - 160) + 'px';
}
};//window.pageYOffset +
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment