Come scoprire se iPad è in modalità orizzontale / verticale in javascript / jquery?
-
29-09-2019 - |
Domanda
Voglio aggiungere un div extra se l'iPad è in modalità orizzontale. C'è una sorta di se dichiarazione che potrebbe scoprire questo?
Grazie
Soluzione
jQTouch controlli in questo modo:
orientation = Math.abs(window.orientation) == 90 ? 'landscape' : 'portrait';
http://github.com/senchalabs/jQTouch/blob /master/jqtouch/jqtouch.js
È anche possibile ascoltare onOrientationChange eventi
Si veda la risposta precedente: rilevare la rotazione del telefono Android nel browser con JavaScript
Altri suggerimenti
Si potrebbe fare un semplice controllo per la larghezza del documento.
$(window).width();
Si potrebbe fissare ad una variabile, e quindi controllare la variabile contro la risoluzione nativa del iPad:. 768px x 1024px nel ritratto
<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>
è possibile provare la soluzione, compatibile con tutti i browser.
In seguito è orientationchange
compatibilità pic:
pertanto, autore polyfill orientaionchange
, si basa su un @media attributo per utilità correzione orientationChange library-- orientationChange -fix
window.addEventListener('orientationchange', function(){
if(window.neworientation.current === 'portrait|landscape'){
// do something……
} else {
// do something……
}
}, false);
e poi, è possibile recuperare lo stato attuale delle orientamento con window.neworientation.current
e stato iniziale di orientamento con window.neworientation.init
.