orientation_compass.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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, x;
  29. var ld;
  30. var tran="0.1s", empty="0s";
  31. var bani=false, bsp=true;
  32. function update(d) {
  33. var c = ld - d;
  34. var ac = Math.abs(c);
  35. if (ac <= 1) {
  36. return;
  37. } else if (ac > 180) {
  38. if ( bani ) {
  39. t.style.webkitTransitionDuration = empty;
  40. }
  41. ld = (c > 0) ? ld - 360 : ld + 360;
  42. t.style.webkitTransform = "rotate(" + (bsp?(-ld):(ld)) + "deg)";
  43. setTimeout(adjust, 1);
  44. } else {
  45. t.style.webkitTransform = "rotate(" + (bsp?(-d):(d)) + "deg)";
  46. }
  47. ld = d;
  48. var r = Math.round(d);
  49. var txt = "";
  50. if (r >= 338 || r < 23) {
  51. txt = "北";
  52. } else if (r < 68) {
  53. txt = "东北";
  54. } else if (r < 113) {
  55. txt = "东";
  56. } else if (r < 158) {
  57. txt = "东南";
  58. } else if (r < 203) {
  59. txt = "南";
  60. } else if (r < 248) {
  61. txt = "西南";
  62. } else if (r < 293) {
  63. txt = "西";
  64. } else {
  65. txt = "西北";
  66. }
  67. x.innerHTML = txt + r + "°";
  68. }
  69. function adjust() {
  70. if ( bani ) {
  71. t.style.webkitTransitionDuration = tran;
  72. }
  73. t.style.webkitTransform = "rotate(" + (bsp?(-ld):(ld)) + "deg)";
  74. }
  75. function gohome() {
  76. plus.webview.currentWebview().close();
  77. }
  78. document.addEventListener("plusready", function() {
  79. t = document.getElementById("compass");
  80. x = document.getElementById("inf");
  81. if ( plus.os.name == "iOS" ) {
  82. t.style.webkitTransition = "all 0.1s linear";
  83. bani = true;
  84. if ( plus.device.model.indexOf("iPhone") < 0 ) {
  85. bsp = false;
  86. document.getElementById("tip").innerText = "此设备方向数据不准确";
  87. }
  88. }
  89. plus.orientation.watchOrientation(function(r) {
  90. update(bsp?r.trueHeading:r.alpha);//update(r.alpha);
  91. }, function(e) {
  92. // error
  93. }, {
  94. frequency : 100
  95. });
  96. }, false);
  97. // Adjective select.
  98. document.onselectstart = function() {
  99. return false;
  100. }
  101. </script>
  102. <link rel="stylesheet" href="../css/common.css" type="text/css" charset="utf-8"/>
  103. <style type="text/css">
  104. #front {
  105. width: 100%;
  106. height: 40px;
  107. text-align: center;
  108. margin-top: 30px;
  109. }
  110. #container {
  111. width: 80%;
  112. position: relative;
  113. margin: auto;
  114. margin-top: 10px;
  115. text-align: center;
  116. line-height: 100%;
  117. overflow: hidden;
  118. }
  119. #bg {
  120. width: 100%;
  121. position: absolute;
  122. left: 0px;
  123. top: 0px;
  124. z-index: -1;
  125. }
  126. #compass {
  127. width: 100%;
  128. vertical-align: middle;
  129. /*
  130. -webkit-transition: all 0.1s linear;
  131. -moz-transform: rotate(45deg);
  132. -webkit-transform: rotate(45deg);
  133. -o-transform: rotate(45deg);
  134. -ms-transform: rotate(45deg);
  135. transform: rotate(45deg);
  136. */
  137. }
  138. #inf {
  139. width: 100%;
  140. height: 40px;
  141. line-height: 40px;
  142. font-size: 32px;
  143. font-weight: bold;
  144. text-align: center;
  145. }
  146. a.bh {
  147. width: 100%;
  148. height: 40px;
  149. position: fixed;
  150. bottom: 0px;
  151. background: rgb(128,128,128);
  152. text-align: center;
  153. line-height: 32px;
  154. }
  155. a.bh:active {
  156. background: rgb(64,64,64);
  157. }
  158. #hi {
  159. width: 32px;
  160. height: 32px;
  161. vertical-align: middle;
  162. }
  163. </style>
  164. </head>
  165. <body>
  166. <div id="front">
  167. <img style="height:100%;" src="../img/orientationa.png"/>
  168. </div>
  169. <div id="inf">
  170. 北0°
  171. </div>
  172. <div id="container">
  173. <img id="bg" src="../img/orientationbg.png"/>
  174. <img id="compass" src="../img/orientationc.png"/>
  175. </div>
  176. <div id="tip" style="width:100%;position:absolute;bottom:0;text-align:center;color:red"></div>
  177. </body>
  178. </html>