on-compass-change.vue 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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:50rpx;">
  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)'"></image>
  11. <view class="direction-value">
  12. <text>{{direction}}</text>
  13. <text class="direction-degree">o</text>
  14. </view>
  15. </view>
  16. </view>
  17. </view>
  18. </template>
  19. <script>
  20. export default {
  21. data() {
  22. return {
  23. title: 'onCompassChange',
  24. direction: 0
  25. }
  26. },
  27. onReady: function () {
  28. uni.onCompassChange((res) => {
  29. this.direction = parseInt(res.direction)
  30. })
  31. },
  32. onUnload() {
  33. // #ifndef MP-ALIPAY
  34. uni.stopCompass();
  35. this.direction = 0;
  36. // #endif
  37. // #ifdef MP-ALIPAY
  38. uni.offCompassChange();
  39. // #endif
  40. }
  41. }
  42. </script>
  43. <style>
  44. .direction {
  45. position: relative;
  46. margin-top: 70rpx;
  47. display: flex;
  48. width: 540rpx;
  49. height: 540rpx;
  50. align-items: center;
  51. justify-content: center;
  52. margin:0 auto;
  53. }
  54. .direction-value {
  55. position: relative;
  56. font-size: 200rpx;
  57. color: #353535;
  58. line-height: 1;
  59. z-index: 1;
  60. }
  61. .direction-degree {
  62. position: absolute;
  63. top: 0;
  64. right: -40rpx;
  65. font-size: 60rpx;
  66. }
  67. .bg-compass {
  68. position: absolute;
  69. top: 0;
  70. left: 0;
  71. width: 540rpx;
  72. height: 540rpx;
  73. transition: .1s;
  74. }
  75. .bg-compass-line {
  76. position: absolute;
  77. left: 267rpx;
  78. top: -10rpx;
  79. width: 6rpx;
  80. height: 56rpx;
  81. background-color: #1AAD19;
  82. border-radius: 999rpx;
  83. z-index: 1;
  84. }
  85. </style>