Server.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <template>
  2. <view>
  3. <view
  4. @click="navTo"
  5. class="serve"
  6. :style="{ left: left + 'rpx', top: top + 'rpx' }"
  7. @touchstart="onTouchStart"
  8. @touchmove="onTouchMove"
  9. @touchend="onTouchEnd"
  10. >
  11. <view class="serve-content">
  12. <view>邀请</view>
  13. <view>讲者</view>
  14. </view>
  15. </view>
  16. </view>
  17. </template>
  18. <script>
  19. export default {
  20. name:"Server",
  21. data() {
  22. return {
  23. left: 0,
  24. top: 0,
  25. startX: 0,
  26. startY: 0,
  27. isDragging: false,
  28. screenWidth: 0,
  29. screenHeight: 0
  30. };
  31. },
  32. mounted() {
  33. this.getSystemInfo();
  34. this.initPosition();
  35. },
  36. methods: {
  37. navTo(){
  38. uni.navigateTo({
  39. url: '/pages_speaker/speakerInvitation'
  40. })
  41. },
  42. // 初始化位置
  43. initPosition() {
  44. // 从本地存储加载位置
  45. const position = uni.getStorageSync('server_position');
  46. if (position) {
  47. this.left = position.left;
  48. this.top = position.top;
  49. } else {
  50. // 默认位置:右侧80%高度
  51. this.left = 750 - 150 - 20; // 屏幕宽度 - 容器宽度 - 右边距
  52. this.top = this.px2rpx(this.screenHeight * 0.8 - 75); // 80%高度,居中偏移
  53. }
  54. },
  55. // 获取系统信息
  56. getSystemInfo() {
  57. uni.getSystemInfo({
  58. success: (res) => {
  59. this.screenWidth = res.windowWidth;
  60. this.screenHeight = res.windowHeight;
  61. }
  62. });
  63. },
  64. // 触摸开始
  65. onTouchStart(e) {
  66. // 阻止事件冒泡,不影响其他元素
  67. e.stopPropagation();
  68. e.preventDefault();
  69. this.startX = e.touches[0].clientX;
  70. this.startY = e.touches[0].clientY;
  71. this.isDragging = false;
  72. },
  73. // 触摸移动
  74. onTouchMove(e) {
  75. // 阻止事件冒泡和默认行为
  76. e.stopPropagation();
  77. e.preventDefault();
  78. if (!this.startX || !this.startY) return;
  79. const currentX = e.touches[0].clientX;
  80. const currentY = e.touches[0].clientY;
  81. const diffX = currentX - this.startX;
  82. const diffY = currentY - this.startY;
  83. // 判断是否开始拖拽(移动超过5px)
  84. if (!this.isDragging && (Math.abs(diffX) > 5 || Math.abs(diffY) > 5)) {
  85. this.isDragging = true;
  86. }
  87. if (this.isDragging) {
  88. // 计算新的位置(rpx)
  89. const newLeft = this.rpx2px(this.left) + diffX;
  90. const newTop = this.rpx2px(this.top) + diffY;
  91. // 边界检查
  92. const boundedLeft = Math.max(10, Math.min(newLeft, this.screenWidth - this.rpx2px(150)));
  93. const boundedTop = Math.max(10, Math.min(newTop, this.screenHeight - this.rpx2px(150)));
  94. // 转换回rpx
  95. this.left = this.px2rpx(boundedLeft);
  96. this.top = this.px2rpx(boundedTop);
  97. // 更新起始位置
  98. this.startX = currentX;
  99. this.startY = currentY;
  100. }
  101. },
  102. // 触摸结束
  103. onTouchEnd(e) {
  104. // 阻止事件冒泡
  105. e.stopPropagation();
  106. e.preventDefault();
  107. if (this.isDragging) {
  108. // 吸边效果
  109. this.attachToEdge();
  110. // 保存位置
  111. this.savePosition();
  112. }
  113. this.startX = 0;
  114. this.startY = 0;
  115. this.isDragging = false;
  116. },
  117. // 吸边效果
  118. attachToEdge() {
  119. const containerWidth = 150; // 容器宽度
  120. const screenWidthRpx = 750;
  121. const centerX = screenWidthRpx / 2;
  122. // 根据当前位置决定吸附到左边还是右边
  123. if (this.left < centerX) {
  124. this.left = 20; // 吸附到左边
  125. } else {
  126. this.left = screenWidthRpx - containerWidth - 20; // 吸附到右边
  127. }
  128. },
  129. // 保存位置到本地存储
  130. savePosition() {
  131. uni.setStorageSync('server_position', {
  132. left: this.left,
  133. top: this.top
  134. });
  135. },
  136. // rpx转px
  137. rpx2px(rpx) {
  138. return rpx / 750 * this.screenWidth;
  139. },
  140. // px转rpx
  141. px2rpx(px) {
  142. return px * 750 / this.screenWidth;
  143. }
  144. }
  145. }
  146. </script>
  147. <style lang="scss" scoped>
  148. .serve{
  149. position: fixed;
  150. z-index: 9;
  151. width: 96rpx;
  152. height: 96rpx;
  153. background: linear-gradient( 0deg, #388BFF 0%, #388BFF 100%);
  154. box-shadow: inset 0rpx 4rpx 8rpx 0rpx rgba(255,255,255,0.25), inset 0rpx -6rpx 8rpx 0rpx rgba(255,255,255,0.25), 4rpx 8rpx 12rpx 0rpx rgba(88,144,239,0.29);
  155. border-radius: 110rpx 110rpx 110rpx 110rpx;
  156. // 确保拖拽时不影响其他元素
  157. touch-action: none;
  158. user-select: none;
  159. -webkit-user-select: none;
  160. .serve-content {
  161. width: 100%;
  162. height: 100%;
  163. display: flex;
  164. flex-direction: column;
  165. align-items: center;
  166. justify-content: center;
  167. position: relative;
  168. font-weight: 500;
  169. font-size: 24rpx;
  170. color: #FFFFFF;
  171. // 拖拽时的视觉反馈
  172. &:active {
  173. opacity: 0.9;
  174. transition: opacity 0.2s ease;
  175. }
  176. }
  177. .w130 {
  178. width: 130rpx !important;
  179. height: 130rpx !important;
  180. flex-shrink: 0; // 防止图片变形
  181. }
  182. .h130 {
  183. width: 130rpx !important;
  184. height: 130rpx !important;
  185. flex-shrink: 0; // 防止图片变形
  186. }
  187. .text{
  188. margin-top: 8rpx;
  189. font-weight: 500;
  190. font-size: 28rpx;
  191. color: #333;
  192. background: rgba(255, 255, 255, 0.9);
  193. padding: 6rpx 12rpx;
  194. border-radius: 20rpx;
  195. white-space: nowrap;
  196. flex-shrink: 0; // 防止文字变形
  197. border: 1rpx solid rgba(0, 0, 0, 0.1);
  198. backdrop-filter: blur(10rpx);
  199. }
  200. .contact-btn {
  201. position: absolute;
  202. top: 0;
  203. left: 0;
  204. width: 100%;
  205. height: 100%;
  206. opacity: 0;
  207. z-index: 10;
  208. // 确保按钮不变形
  209. border: none;
  210. border-radius: 0;
  211. background: transparent;
  212. padding: 0;
  213. margin: 0;
  214. &::after {
  215. border: none;
  216. }
  217. }
  218. }
  219. // 防止拖拽时页面滚动
  220. page {
  221. overflow: hidden;
  222. height: 100%;
  223. }
  224. </style>