compass.uvue 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <template>
  2. <view>
  3. <page-head :title="title"></page-head>
  4. <view class="uni-padding-wrap">
  5. <view class="uni-hello-text uni-center" style="padding-bottom:25px;">
  6. 旋转手机即可获取方位信息
  7. </view>
  8. <view class="direction">
  9. <view class="bg-compass-line"></view>
  10. <image class="bg-compass" src="../../../static/compass.png" :style="'transform: rotate('+direction+'deg)'">
  11. </image>
  12. <view class="direction-value">
  13. <text>{{direction}}</text>
  14. <text class="direction-degree">o</text>
  15. </view>
  16. </view>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. export default {
  22. data() {
  23. return {
  24. title: 'onCompassChange',
  25. direction: 0
  26. }
  27. },
  28. onReady: function () {
  29. uni.onCompassChange((res) => {
  30. console.log('onCompassChange', res)
  31. this.direction = res.direction
  32. })
  33. },
  34. onUnload() {
  35. uni.stopCompass();
  36. this.direction = 0;
  37. }
  38. }
  39. </script>
  40. <style>
  41. .direction {
  42. position: relative;
  43. margin-top: 35px;
  44. display: flex;
  45. width: 270px;
  46. height: 270px;
  47. align-items: center;
  48. justify-content: center;
  49. margin: 0 auto;
  50. }
  51. .direction-value {
  52. position: relative;
  53. font-size: 100px;
  54. color: #353535;
  55. line-height: 1;
  56. z-index: 1;
  57. }
  58. .direction-degree {
  59. position: absolute;
  60. top: 0;
  61. right: -20px;
  62. font-size: 30px;
  63. }
  64. .bg-compass {
  65. position: absolute;
  66. top: 0;
  67. left: 0;
  68. width: 270px;
  69. height: 270px;
  70. transition: .1s;
  71. }
  72. .bg-compass-line {
  73. position: absolute;
  74. left: 134px;
  75. top: -5px;
  76. width: 3px;
  77. height: 28px;
  78. background-color: #1AAD19;
  79. border-radius: 100px;
  80. z-index: 1;
  81. }
  82. </style>