orientation_level.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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>水平仪</title>
  9. <script type="text/javascript" src="../js/common.js"></script>
  10. <script type="text/javascript" >
  11. // H5 plus事件处理
  12. function plusReady(){
  13. // 锁定只能竖屏显示
  14. plus.screen.lockOrientation( "portrait-primary" );
  15. }
  16. if(window.plus){
  17. plusReady();
  18. }else{
  19. document.addEventListener("plusready",plusReady,false);
  20. }
  21. // 解锁并关闭
  22. var _back=window.back;
  23. function unlockback(){
  24. plus.screen.unlockOrientation();
  25. _back();
  26. }
  27. window.back=unlockback;
  28. var t, xh, xv;
  29. var R = 100;
  30. function update(h, v) {
  31. var ch = 0, cv = 0;
  32. if (h > 90) {
  33. h = 180 - h;
  34. } else if (h < -90) {
  35. h = -180 - h;
  36. }
  37. if (v > 90) {
  38. v = 180 - v;
  39. } else if (v < -90) {
  40. v = -180 - v;
  41. }
  42. var r = Math.max(Math.abs(h), Math.abs(v)) / 90;
  43. var range = Math.round(R * r);
  44. if (h == 0) {
  45. cv = range * v / 90;
  46. } else if (v == 0) {
  47. ch = range * h / 90;
  48. } else {
  49. var a = Math.atan2(v, h);
  50. ch = range * Math.cos(a);
  51. cv = range * Math.sin(a);
  52. }
  53. ch = Math.round(ch);
  54. cv = Math.round(cv);
  55. t.style.webkitTransform = "translate(" + ch + "px," + cv + "px)";
  56. h = h.toFixed(1);
  57. v = v.toFixed(1);
  58. xh.innerHTML = "" + (-h);
  59. xv.innerHTML = "" + (-v);
  60. }
  61. document.addEventListener("plusready", function() {
  62. t = document.getElementById("bubble");
  63. R = Math.floor(t.offsetWidth * 130 / 320);
  64. xh = document.getElementById("h");
  65. xv = document.getElementById("v");
  66. plus.orientation.watchOrientation(function(r) {
  67. update(-r.gamma, -r.beta);
  68. }, function(e) {
  69. // error
  70. }, {
  71. frequency : 100
  72. });
  73. }, false);
  74. // Adjective select.
  75. document.onselectstart = function() {
  76. return false;
  77. }
  78. </script>
  79. <link rel="stylesheet" href="../css/common.css" type="text/css" charset="utf-8"/>
  80. <style type="text/css">
  81. #front {
  82. width: 60%;
  83. height: 80px;
  84. margin: auto;
  85. margin-top: 20px;
  86. text-align: center;
  87. }
  88. .inf {
  89. width: 50%;
  90. height: 50%;
  91. line-height: 40px;
  92. font-size: 28px;
  93. text-align: center;
  94. text-align: center;
  95. vertical-align: middle;
  96. float: left;
  97. }
  98. #container {
  99. width: 80%;
  100. position: relative;
  101. margin: auto;
  102. text-align: center;
  103. line-height: 100%;
  104. overflow: hidden;
  105. }
  106. .bg {
  107. width: 100%;
  108. position: absolute;
  109. left: 0px;
  110. top: 0px;
  111. z-index: -1;
  112. }
  113. #bubble {
  114. width: 100%;
  115. vertical-align: middle;
  116. -webkit-transition: all 0.1s linear;
  117. }
  118. a.bh {
  119. width: 100%;
  120. height: 10%;
  121. position: fixed;
  122. bottom: 0px;
  123. background: rgb(128,128,128);
  124. text-align: center;
  125. vertical-align: middle;
  126. }
  127. a.bh:active {
  128. background: rgb(64,64,64);
  129. }
  130. #hi {
  131. height: 80%;
  132. margin: auto;
  133. margin-top: 5px;
  134. vertical-align: middle;
  135. }
  136. </style>
  137. </head>
  138. <body>
  139. <div id="front">
  140. <div class="inf" style="font-weight:bold;">
  141. 横向
  142. </div>
  143. <div class="inf" style="font-weight:bold;">
  144. 纵向
  145. </div>
  146. <div id="h" class="inf">
  147. 0
  148. </div>
  149. <span id="v" class="inf">0</span>
  150. </div>
  151. <div id="container">
  152. <img class="bg" src="../img/orientationbg.png"/>
  153. <img class="bg" src="../img/orientationl.png"/>
  154. <img id="bubble" src="../img/orientationb.png"/>
  155. </div>
  156. </body>
  157. </html>