on-compass-change.uvue 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. console.log('onCompassChange', res)
  30. this.direction = res.direction
  31. })
  32. },
  33. onUnload() {
  34. uni.stopCompass();
  35. this.direction = 0;
  36. }
  37. }
  38. </script>
  39. <style>
  40. .direction {
  41. position: relative;
  42. margin-top: 70rpx;
  43. display: flex;
  44. width: 540rpx;
  45. height: 540rpx;
  46. align-items: center;
  47. justify-content: center;
  48. margin:0 auto;
  49. }
  50. .direction-value {
  51. position: relative;
  52. font-size: 200rpx;
  53. color: #353535;
  54. line-height: 1;
  55. z-index: 1;
  56. }
  57. .direction-degree {
  58. position: absolute;
  59. top: 0;
  60. right: -40rpx;
  61. font-size: 60rpx;
  62. }
  63. .bg-compass {
  64. position: absolute;
  65. top: 0;
  66. left: 0;
  67. width: 540rpx;
  68. height: 540rpx;
  69. transition: .1s;
  70. }
  71. .bg-compass-line {
  72. position: absolute;
  73. left: 267rpx;
  74. top: -10rpx;
  75. width: 6rpx;
  76. height: 56rpx;
  77. background-color: #1AAD19;
  78. border-radius: 999rpx;
  79. z-index: 1;
  80. }
  81. </style>