Server.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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: 9999;
  151. // width: 150rpx;
  152. // height: 150rpx;
  153. width: 96rpx;
  154. height: 96rpx;
  155. background: linear-gradient( 0deg, #388BFF 0%, #388BFF 100%);
  156. 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);
  157. border-radius: 110rpx 110rpx 110rpx 110rpx;
  158. // 确保拖拽时不影响其他元素
  159. touch-action: none;
  160. user-select: none;
  161. -webkit-user-select: none;
  162. .serve-content {
  163. width: 100%;
  164. height: 100%;
  165. display: flex;
  166. flex-direction: column;
  167. align-items: center;
  168. justify-content: center;
  169. position: relative;
  170. font-weight: 500;
  171. font-size: 24rpx;
  172. color: #FFFFFF;
  173. // 拖拽时的视觉反馈
  174. &:active {
  175. opacity: 0.9;
  176. transition: opacity 0.2s ease;
  177. }
  178. }
  179. .w130 {
  180. width: 130rpx !important;
  181. height: 130rpx !important;
  182. flex-shrink: 0; // 防止图片变形
  183. }
  184. .h130 {
  185. width: 130rpx !important;
  186. height: 130rpx !important;
  187. flex-shrink: 0; // 防止图片变形
  188. }
  189. .text{
  190. margin-top: 8rpx;
  191. font-weight: 500;
  192. font-size: 28rpx;
  193. color: #333;
  194. background: rgba(255, 255, 255, 0.9);
  195. padding: 6rpx 12rpx;
  196. border-radius: 20rpx;
  197. white-space: nowrap;
  198. flex-shrink: 0; // 防止文字变形
  199. border: 1rpx solid rgba(0, 0, 0, 0.1);
  200. backdrop-filter: blur(10rpx);
  201. }
  202. .contact-btn {
  203. position: absolute;
  204. top: 0;
  205. left: 0;
  206. width: 100%;
  207. height: 100%;
  208. opacity: 0;
  209. z-index: 10;
  210. // 确保按钮不变形
  211. border: none;
  212. border-radius: 0;
  213. background: transparent;
  214. padding: 0;
  215. margin: 0;
  216. &::after {
  217. border: none;
  218. }
  219. }
  220. }
  221. // 防止拖拽时页面滚动
  222. page {
  223. overflow: hidden;
  224. height: 100%;
  225. }
  226. </style>