accelerometer_shake.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="utf-8"/>
  5. <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
  6. <meta name="HandheldFriendly" content="true"/>
  7. <meta name="MobileOptimized" content="320"/>
  8. <title>Hello H5+</title>
  9. <script type="text/javascript" src="../js/common.js"></script>
  10. <script type="text/javascript">
  11. var p=null;
  12. var iLast=2,MAX=20;
  13. var up=null,down=null,offset=50;
  14. var t=null;
  15. // H5 plus事件处理
  16. function plusReady(){
  17. // 锁定只能竖屏显示
  18. plus.screen.lockOrientation('portrait-primary');
  19. // 监听加速度
  20. plus.accelerometer.watchAcceleration(function(a){
  21. if(!up){
  22. return;
  23. }
  24. if(!p && ( Math.abs(a.xAxis)+Math.abs(a.yAxis)+Math.abs(a.zAxis) > MAX )){
  25. // Play audio
  26. p = plus.audio.createPlayer('_www/audio/shake.wav');
  27. p.play();
  28. setTimeout(function(){
  29. // Change background image
  30. var index = Math.round(Math.random()*3+1);
  31. if ( iLast == index ) {
  32. index++;
  33. if ( index > 4 ) {
  34. index = 1;
  35. }
  36. }
  37. document.body.style.backgroundImage='url(../img/shake/'+index+'.jpg)';
  38. iLast = index;
  39. // Stop play audio
  40. p.stop();
  41. delete p;
  42. p = null;
  43. }, 2000 );
  44. // Animation
  45. offset=up.offsetHeight/2;
  46. up.style.webkitTransform = 'translateY(-'+offset+'px)';
  47. up.style.msTransform = 'translateY(-'+offset+'px)';
  48. down.style.webkitTransform = 'translateY('+offset+'px)';
  49. down.style.msTransform = 'translateY('+offset+'px)';
  50. if(t){
  51. clearTimeout(t);
  52. }
  53. t = setTimeout(function(){
  54. t = null;
  55. up.style.webkitTransform = '';
  56. up.style.msTransform = '';
  57. down.style.webkitTransform = '';
  58. down.style.msTransform = '';
  59. }, 700 );
  60. }
  61. }, function(e){
  62. //outSet('Watch failed: '+e.message);
  63. }, {frequency:100});
  64. }
  65. if(window.plus){
  66. plusReady();
  67. }else{
  68. document.addEventListener('plusready', plusReady, false);
  69. }
  70. // 监听DOMContentLoaded事件
  71. document.addEventListener('DOMContentLoaded', function(){
  72. up=document.getElementById('up');
  73. down=document.getElementById('down');
  74. offset=up.offsetHeight/2;
  75. },false);
  76. // 解锁并关闭
  77. var _back=window.back;
  78. function unlockback(){
  79. plus.screen.unlockOrientation();
  80. _back();
  81. }
  82. window.back=unlockback;
  83. </script>
  84. <link rel="stylesheet" href="../css/common.css" type="text/css" charset="utf-8"/>
  85. </head>
  86. <body style="background: center center no-repeat url(../img/shake/1.jpg);">
  87. <div style="height:100%;text-align:center;overflow:hidden;">
  88. <div id="up" style="width:100%;height:50%;background:#333;-webkit-transition:all .5s ease-in-out;-ms-transition:all .5s ease-in-out;">
  89. <img style="height:100%;" src="../img/shakeup.png"/>
  90. </div>
  91. <div id="down" style="padding-bottom:20px;width:100%;height:50%;background:#333;-webkit-transition:all .5s ease-in-out;-ms-transition:all .5s ease-in-out;">
  92. <img style="height:100%;" src="../img/shakedown.png"/>
  93. </div>
  94. </div>
  95. </body>
  96. </html>