Created
November 7, 2024 00:38
-
-
Save jonstuebe/6d8871c645e3e1a7d62933224e338073 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function isIOS() { | |
// deprecated api but still exists in mobile safari for now | |
if (navigator.platform) { | |
return ( | |
[ | |
"iPad Simulator", | |
"iPhone Simulator", | |
"iPod Simulator", | |
"iPad", | |
"iPhone", | |
"iPod", | |
].includes(navigator.platform) || | |
// iPad on iOS 13 detection | |
(navigator.userAgent.includes("Mac") && "ontouchend" in document) | |
); | |
} | |
// weak test | |
return ( | |
/iPad|iPhone|iPod/.test(navigator.userAgent) && | |
window.navigator.vendor === "Apple Computer, Inc." | |
); | |
} | |
export function disableIOSPinchZoom() { | |
if (isIOS()) { | |
// disable pinch zoom | |
document.addEventListener( | |
"touchmove", | |
function (event: any) { | |
if (event.scale !== 1) { | |
event.preventDefault(); | |
} | |
}, | |
{ passive: false } | |
); | |
} | |
} | |
export function disableIOSDoubleTapZoom() { | |
if (isIOS()) { | |
// disable double tap to zoom | |
let lastTouchEnd = 0; | |
document.addEventListener( | |
"touchend", | |
function (event: any) { | |
const now = new Date().getTime(); | |
if (now - lastTouchEnd <= 300) { | |
event.preventDefault(); | |
} | |
lastTouchEnd = now; | |
}, | |
false | |
); | |
} | |
} | |
export function disableIOSZoom() { | |
disableIOSPinchZoom(); | |
disableIOSDoubleTapZoom(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment