NetPlusMédia - Obtenir l'orientation du téléphone en javascript
NetPlusMédia

Obtenir l'orientation du téléphone en javascript

Retour

Source

 

Veuillez utiliser un téléphone ou une tableau pour tester ce script

 

qrcode

orientation-telephone.html

  1. <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN'
  2. 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
  3. <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='fr' lang='fr'>
  4. <title>Obtenir l&#39;orientation du t&eacute;l&eacute;phone en javascript</title>
  5. <meta http-equiv='content-type' content='text/html; charset=iso-8859-1'/>
  6.  
  7.  
  8. <!-- Style CSS -->
  9.  
  10.  
  11.  
  12. </head>
  13.  
  14.  
  15. <div id="log"></div>
  16. <br />
  17. Si des valeurs <code>null</code> sont affichées, le matériel ne supporte pas l'API Motion
  18.  
  19.  
  20. <!-- Javascript -->
  21. <script language='Javascript' type='text/javascript'
  22. src='orientation-telephone.js'></script>
  23.  
  24. </body>
  25. </html>

orientation-telephone.js

  1. function process(event) {
  2.  
  3. var x = event.accelerationIncludingGravity.x;
  4. var y = event.accelerationIncludingGravity.y;
  5. var z = event.accelerationIncludingGravity.z;
  6.  
  7. document.getElementById('log').innerHTML = "X : " + x + "<br />Y : " + y + "<br />Z : " + z;
  8.  
  9. }
  10.  
  11. window.addEventListener("devicemotion", process, true);

Démonstration

Obtenir l'orientation du téléphone en javascript

Si des valeurs null sont affichées, le matériel ne supporte pas l'API Motion
Plan du site - Contact - Google+ - FaceBook - Twitter