Comment savoir si l'iPad est en mode paysage / portrait en javascript / jquery?
-
29-09-2019 - |
Question
Je veux ajouter un div supplémentaire si l'iPad est en mode paysage. Y at-il une sorte de déclaration qui pourrait se trouver cela?
Merci
La solution
vérifie jQTouch comme ceci:
orientation = Math.abs(window.orientation) == 90 ? 'landscape' : 'portrait';
http://github.com/senchalabs/jQTouch/blob /master/jqtouch/jqtouch.js
Vous pouvez également écouter onOrientationChange événements
Voir la réponse précédente: Détecte rotation du téléphone Android dans le navigateur avec JavaScript
Autres conseils
Vous pourriez faire une simple vérification de la largeur du document.
$(window).width();
vous pouvez définir à une variable, puis vérifiez la variable par rapport à la résolution native de l'iPad. 768px x 1024px en mode portrait
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Rotation Test</title>
<link type="text/css" href="css/style.css" rel="stylesheet"></style>
<script src="js/jquery-1.5.min.js" type="text/javascript"></script>
<script type="text/javascript">
window.addEventListener("resize", function() {
// Get screen size (inner/outerWidth, inner/outerHeight)
var height = $(window).height();
var width = $(window).width();
if(width>height) {
// Landscape
$("#mode").text("LANDSCAPE");
} else {
// Portrait
$("#mode").text("PORTRAIT");
}
}, false);
</script>
</head>
<body onorientationchange="updateOrientation();">
<div id="mode">LANDSCAPE</div>
</body>
</html>
vous pouvez essayer la solution, compatible avec tous les navigateurs.
est la suite pic de compatibilité orientationchange
:
donc, je l'auteur d'un polyfill orientaionchange
, il est basé sur @media attribut fix orientationChange utilitaire library-- orientationChange -fix
window.addEventListener('orientationchange', function(){
if(window.neworientation.current === 'portrait|landscape'){
// do something……
} else {
// do something……
}
}, false);
et puis, vous pouvez récupérer l'état actuel de l'orientation par window.neworientation.current
et de l'état initial de l'orientation par window.neworientation.init
.