Skip to content

Instantly share code, notes, and snippets.

@davorpa
Last active September 11, 2021 06:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davorpa/62b1a394738da893c8069cf8f411d145 to your computer and use it in GitHub Desktop.
Save davorpa/62b1a394738da893c8069cf8f411d145 to your computer and use it in GitHub Desktop.
A easy script to put at your HTML body end to fix scale gestures on iOS devices
!(function(document, undefined) { // IIFE::start
if (!navigator.userAgent.match(/iPhone/i)) return; // only for iPhone
var metas = document.getElementsByTagName("meta"),
NORMAL_SCALE = 1.0,
MIN_SCALE = 0.25,
MAX_SCALE = 1.6;
initialize();
// document.addEventListener("gesturestart", onGestureStart, false);
document.addEventListener("touchstart", onGestureStart, false);
document.addEventListener("touchend", onGestureEnd, false);
function changeViewportContent(content) {
for (var i = 0; i < metas.length; i++) {
if (metas[i].name == "viewport") {
metas[i].content = content;
}
}
}
function setViewportScale(min, max) {
for (var i = 0; i < metas.length; i++) {
if (metas[i].name == "viewport") {
metas[i].content = "width=device-width, minimum-scale=" + (min) + ", maximum-scale=" + (max);
}
}
}
function initialize() {
changeViewportContent("width=device-width, minimum-scale=1.0, maximum-scale=1.0");
}
function onGestureStart(event) {
changeViewportContent("width=device-width, minimum-scale=0.25, maximum-scale=1.6");
}
function onGestureEnd(event) {
initialize();
};
})(document); // IIFE::end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment