feedback.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <template>
  2. <div class="compass-container">
  3. <div class="compass" :style="compassStyle">
  4. <div class="arrow"></div>
  5. </div>
  6. <p>当前方向: {{ direction }}°</p>
  7. </div>
  8. </template>
  9. <script>
  10. export default {
  11. data() {
  12. return {
  13. direction: 0, // 当前方向
  14. compassStyle: {
  15. transform: `rotate(${this.direction}deg)` // 初始化旋转角度
  16. }
  17. };
  18. },
  19. mounted() {
  20. // 浏览器支持的设备方向API
  21. if (window.DeviceOrientationEvent) {
  22. window.addEventListener("deviceorientation", this.handleOrientation);
  23. } else {
  24. alert("您的设备不支持设备方向API");
  25. }
  26. },
  27. beforeDestroy() {
  28. // 清理事件监听器
  29. if (window.DeviceOrientationEvent) {
  30. window.removeEventListener("deviceorientation", this.handleOrientation);
  31. }
  32. },
  33. methods: {
  34. handleOrientation(event) {
  35. // 获取 alpha (旋转角度)
  36. let alpha = event.alpha; // 设备的旋转角度
  37. if (alpha !== null) {
  38. this.direction = alpha;
  39. this.compassStyle.transform = `rotate(${this.direction}deg)`; // 更新旋转角度
  40. }
  41. }
  42. }
  43. };
  44. </script>
  45. <style scoped>
  46. .compass-container {
  47. text-align: center;
  48. padding: 20px;
  49. }
  50. .compass {
  51. width: 200px;
  52. height: 200px;
  53. border: 10px solid #444;
  54. border-radius: 50%;
  55. margin: 0 auto;
  56. position: relative;
  57. background-color: #f0f0f0;
  58. display: flex;
  59. justify-content: center;
  60. align-items: center;
  61. }
  62. .arrow {
  63. width: 5px;
  64. height: 50px;
  65. background-color: red;
  66. position: absolute;
  67. top: 25%;
  68. }
  69. </style>