Created
January 7, 2022 19:26
-
-
Save manuelcanga/ff89f725b530a96d5f7e808266480828 to your computer and use it in GitHub Desktop.
Cambio de viewport según la orientación
This file contains 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
/** 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