Skip to content

Instantly share code, notes, and snippets.

@AntonLitvin
Forked from delphinpro/viewport.js
Last active October 18, 2020 13:31
Show Gist options
  • Save AntonLitvin/72dd0490b8bc75f1b6aef721a81d4963 to your computer and use it in GitHub Desktop.
Save AntonLitvin/72dd0490b8bc75f1b6aef721a81d4963 to your computer and use it in GitHub Desktop.
Conditional meta viewport
/*
* Paste into the end <head>
*/
(function(){
var width = screen.width;
var oldViewport = document.querySelector('meta[name="viewport"]');
var viewport = document.createElement('meta');
viewport.setAttribute('name', 'viewport');
viewport.setAttribute('content', 'width=' + (width <= 640 ? '640' : 'device-width'));
document.head.replaceChild(viewport, oldViewport);
})();
// some another solution
if (window.devicePixelRatio !== 1) { // Костыль для определения иных устройств, с коэффициентом отличным от 1
var dpt = window.devicePixelRatio;
var widthM = window.screen.width * dpt;
var widthH = window.screen.height * dpt;
document.write('<meta name="viewport" content="width=' + widthM+ ', height=' + widthH + '">');
}
$(function() {
if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i)) {
var viewportmeta = document.querySelector('meta[name="viewport"]');
if (viewportmeta) {
viewportmeta.content = 'width=device-width, minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0';
document.body.addEventListener('gesturestart', function () {
viewportmeta.content = 'width=device-width, minimum-scale=0.25, maximum-scale=1.6';
}, false);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment