Skip to content

Instantly share code, notes, and snippets.

@manuelcanga
Created January 7, 2022 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manuelcanga/ff89f725b530a96d5f7e808266480828 to your computer and use it in GitHub Desktop.
Save manuelcanga/ff89f725b530a96d5f7e808266480828 to your computer and use it in GitHub Desktop.
Cambio de viewport según la orientación
/** Basado en: https://stackoverflow.com/questions/15040408/achieving-min-width-with-viewport-meta-tag */
document.addEventListener('DOMContentLoaded', function () {
trasweb.orientationchange();
});
trasweb.orientationchange = function (force_orientation) {
var vp = document.getElementById('viewport');
var orientation = window.orientation;
var is_desktop = document.body.classList.contains('desktop');
var max = trasweb.getMax();
if (is_desktop) {
/** Tablet giradas */
if (180 === orientation) {
var ratio = parseInt(max[0], 10) / 1600;
vp.setAttribute('content', 'width=1600, initial-scale=' + ratio + ', maximum-scale=' + ratio + ', user-scalable=yes');
document.body.classList.add('tablet-landscape');
} else if ((-90 === orientation || 90 === orientation) && max[0] <= 1024) {
var ratio = parseInt(max[0], 10) / 1024;
vp.setAttribute('content', 'width=1024, initial-scale=' + ratio + ', maximum-scale=' + ratio + ', user-scalable=yes');
document.body.classList.add('tablet-portrait');
}
return;
}
if (-90 === orientation || 90 === orientation) {
vp.setAttribute('content', 'width=640, initial-scale=1');
document.body.classList.add('landscape');
} else {
vp.setAttribute('content', 'width=360, initial-scale=1.0, maximum-scale=1.0, user-scalable=no');
document.body.classList.add('portrait');
}
};
window.addEventListener('orientationchange', function () {
window.location.reload(true);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment