Skip to content

Instantly share code, notes, and snippets.

@cooltoast
Created July 28, 2017 22:44
Show Gist options
  • Save cooltoast/57f966e7d8f4926d1f9017e306f3c499 to your computer and use it in GitHub Desktop.
Save cooltoast/57f966e7d8f4926d1f9017e306f3c499 to your computer and use it in GitHub Desktop.
Detect when iOS Safari landscape menu opens and closes
/**
* Credit to https://stackoverflow.com/a/29696509
*/
const isMobileSafari = () => {
const ua = window.navigator.userAgent;
const iOS = ua.match(/iPad/i) || ua.match(/iPhone/i);
const webkit = ua.match(/WebKit/i);
return iOS && webkit && !ua.match(/CriOS/i);
};
/**
* Call this from your onresize handler and handle your update(s)
*/
const isSafariLandscapeMenuOpen = () => {
const innerWidth = window.innerWidth,
innerHeight = window.innerHeight,
isLandscape = innerHeight > innerWidth,
bodyOffsetHeight = document.body.offsetHeight;
return isLandscape && isMobileSafari() && (bodyOffsetHeight > innerHeight);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment