mainLand.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <template>
  2. <view class="main-land-container">
  3. <!-- <view class="scroll-container" @touchstart="touchStart" @touchmove="touchMove" @touchend="touchEnd" >
  4. <view class="background" :style="{ transform: `translateX(${translateX}px)` }">
  5. <image class="island-image" src="/static/island/island.png" mode="heightFix"></image>
  6. </view>
  7. </view> -->
  8. <!-- 第三层:背景 -->
  9. <view class="background-layer"></view>
  10. <!-- 第二层:地图 -->
  11. <view class="map-layer" id="mapLayer" :style="{ transform: `translateX(${translateX}px)` }" @touchstart="touchStart" @touchmove="touchMove" @touchend="touchEnd" @mousedown="onmousedown" @mousemove="onmousemove" @mouseup="onmouseup">
  12. <!-- 这里可以放置地图元素(示例:一个方块) -->
  13. <!-- <view style="position: absolute; top: 30%; left: 30%; width: 100px; height: 100px; background: green;"></view> -->
  14. <image class="island-image" src="/static/island/island.png" mode="widthFix" style="width:1536rpx; bottom: 0rpx;left: 0rpx; position: absolute;"></image>
  15. <image class="house-image" src="/static/island/building/4.png" mode="widthFix" style="width:670rpx; left: 980rpx; bottom:530rpx;position: absolute; transform:translate(-50%,50%);"></image>
  16. </view>
  17. <!-- 第一层:UI -->
  18. <view class="ui-layer">
  19. <view class="ui-content">
  20. <button class="dialog-button">打开对话框</button>
  21. </view>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. export default {
  27. components: {},
  28. data() {
  29. return {
  30. // // 背景位置控制
  31. translateX: 0,
  32. startX: 0,
  33. currentX: 0,
  34. isDragging : false,
  35. // maxTranslate: 0,
  36. // // 获取屏幕宽度和背景宽度
  37. screenWidth: 0,
  38. backgroundWidth: 0,
  39. islandHeight: 0
  40. }
  41. },
  42. onLoad() {
  43. let self = this;
  44. // 获取屏幕宽度
  45. uni.getSystemInfo({
  46. success: (res) => {
  47. self.screenWidth = res.windowWidth;
  48. console.log('屏幕宽度:', self.screenWidth);
  49. }
  50. });
  51. },
  52. onShow() {
  53. // this.loadData();
  54. },
  55. onReady() {
  56. // 在组件渲染完成后获取图片尺寸
  57. setTimeout(() => {
  58. this.getImageSize();
  59. }, 300);
  60. },
  61. methods: {
  62. loadData() {
  63. // 可以在这里加载其他数据
  64. },
  65. getImageSize() {
  66. const query = uni.createSelectorQuery().in(this);
  67. query.select('.island-image').boundingClientRect(data => {
  68. if (data) {
  69. // 获取岛屿图片的宽度和高度
  70. this.backgroundWidth = data.width;
  71. this.islandHeight = data.height;
  72. // this.backgroundWidth = 1536;
  73. // this.islandHeight = 1024;
  74. // 设置背景高度为岛屿高度的两倍(在CSS中实现)
  75. // 计算最大可移动距离
  76. this.maxTranslate = this.backgroundWidth - this.screenWidth;
  77. // 初始位置居中
  78. this.translateX = -this.maxTranslate / 2;
  79. // 打印调试信息
  80. console.log('屏幕宽度:', this.screenWidth);
  81. console.log('背景宽度:', this.backgroundWidth);
  82. console.log('岛屿高度:', this.islandHeight);
  83. console.log('屏幕宽度:',this.screenWidth);
  84. console.log('最大可移动距离:',this.maxTranslate);
  85. console.log('岛屿data:', data);
  86. } else {
  87. console.error('未能获取岛屿图片的尺寸');
  88. }
  89. }).exec();
  90. },
  91. // 触摸开始
  92. touchStart(e) {
  93. console.log('----------- touchStart');
  94. this.startX = e.touches[0].clientX;
  95. this.currentX = this.translateX;
  96. console.log('this.startX =',this.startX);
  97. console.log('this.currentX =',this.currentX);
  98. },
  99. // 触摸移动
  100. touchMove(e) {
  101. console.log('----------- touchMove');
  102. const moveX = e.touches[0].clientX - this.startX;
  103. let newTranslateX = this.currentX + moveX;
  104. // 限制移动范围,不让背景两侧露出
  105. // if (newTranslateX > 0) {
  106. // newTranslateX = 0;
  107. // } else if (newTranslateX < -this.maxTranslate) {
  108. // newTranslateX = -this.maxTranslate;
  109. // }
  110. this.translateX = newTranslateX;
  111. console.log('moveX =',moveX);
  112. console.log('this.translateX =',this.translateX);
  113. },
  114. // 触摸结束
  115. touchEnd() {
  116. console.log('----------- touchEnd');
  117. this.currentX = this.translateX;
  118. console.log('this.currentX =',this.currentX);
  119. },
  120. onmousedown(e) {
  121. console.log('----------- onmousedown');
  122. console.log('----------- e',e);
  123. this.isDragging = true;
  124. this.startX = e.clientX;
  125. this.currentX = this.translateX;
  126. mapLayer.style.cursor = 'grabbing';
  127. },
  128. onmousemove(e) {
  129. if(this.isDragging){
  130. console.log('----------- onmousemove');
  131. const moveX = e.clientX - this.startX;
  132. let newTranslateX = this.currentX + moveX;
  133. //限制移动范围,不让背景两侧露出
  134. if (newTranslateX > 0) {
  135. newTranslateX = 0;
  136. } else if (newTranslateX < -this.maxTranslate) {
  137. newTranslateX = -this.maxTranslate;
  138. }
  139. this.translateX = newTranslateX;
  140. console.log('moveX =',moveX);
  141. console.log('this.translateX =',this.translateX);
  142. }
  143. },
  144. onmouseup(e) {
  145. console.log('----------- onmouseup');
  146. this.isDragging = false;
  147. mapLayer.style.cursor = 'grab';
  148. },
  149. }
  150. }
  151. </script>
  152. <style lang="scss" scoped>
  153. @import './mainLand.scss';
  154. </style>