FloatBtn.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <template>
  2. <div class="float-btn-wrap"
  3. :style="levitatedSpherePositionData"
  4. @mousedown.prevent="onSphereDown"
  5. @mouseup.prevent.stop="onSphereUp"
  6. @touchmove.prevent="touchmoveLevitatedSphere"
  7. @mousemove.prevent="touchmoveLevitatedSphere"
  8. @touchend="touchendLevitatedSphere"
  9. @mouseup="touchendLevitatedSphere"
  10. @mouseleave="touchendLevitatedSphere"
  11. @click.prevent.stop="clickHandler"
  12. >
  13. <div class="round-outside">
  14. <div class="round-small">
  15. <div class="status"
  16. :class="{
  17. success: latency < 51,
  18. warning: latency > 50 && latency < 201,
  19. danger: latency > 200
  20. }"
  21. >
  22. <!-- 信号 0-50ms绿色;51-200ms黄色;201-999ms红色;最多显示999ms;变色部分为延迟变色 -->
  23. <span class="short" />
  24. <span class="middle" />
  25. <span class="high" />
  26. </div>
  27. </div>
  28. </div>
  29. </div>
  30. </template>
  31. <script>
  32. export default {
  33. name: 'FloatBtn',
  34. props: {
  35. // 窗口尺寸 高
  36. width: {
  37. type: Number,
  38. default: 0
  39. },
  40. // 窗口尺寸 宽
  41. height: {
  42. type: Number,
  43. default: 0
  44. },
  45. // 网络延迟
  46. latency: {
  47. type: Number,
  48. default: 0,
  49. },
  50. },
  51. data() {
  52. return {
  53. // 悬浮球位置
  54. levitatedSpherePositionData: {},
  55. }
  56. },
  57. mounted() {
  58. this.initLevitatedSpherePositionData();
  59. },
  60. methods: {
  61. // 悬浮球初始化位置
  62. initLevitatedSpherePositionData() {
  63. // 从本地存储中获取悬浮球位置
  64. let levitatedSpherePositionData = localStorage.getItem('levitatedSpherePositionData');
  65. // 悬浮球位置
  66. this.levitatedSpherePositionData = levitatedSpherePositionData ? JSON.parse(levitatedSpherePositionData) : { left: '15px', top: '15px' }
  67. },
  68. // 悬浮球按下事件
  69. onSphereDown(e) {
  70. // 给元素设置鼠标按下状态
  71. e.target.isMousedown = true;
  72. e.preventDefault();
  73. },
  74. // 悬浮球抬起事件
  75. onSphereUp(e) {
  76. // 给元素设置鼠标按下状态
  77. e.target.isMousedown = false;
  78. e.preventDefault();
  79. },
  80. // 悬浮球移动
  81. touchmoveLevitatedSphere(e) {
  82. // 过滤未按下时的移动事件
  83. if (e.type === 'mousemove' && !e.target.isMousedown) return
  84. let pageX, pageY;
  85. if (e.type === 'mousemove' && e.target.isMousedown) {
  86. pageX = e.pageX;
  87. pageY = e.pageY;
  88. } else if (e.type === 'touchmove') {
  89. pageX = e.targetTouches[0].pageX;
  90. pageY = e.targetTouches[0].pageY;
  91. }
  92. let min = 20
  93. let MaxPageX = this.width - 20
  94. let MaxPageY = this.height - 20
  95. pageX = pageX <= min ? min : (pageX >= MaxPageX ? MaxPageX : pageX)
  96. pageY = pageY <= min ? min : (pageY >= MaxPageY ? MaxPageY : pageY)
  97. this.levitatedSpherePositionData = {
  98. left: `${pageX}px`,
  99. top: `${pageY}px`,
  100. transform: 'translate(-50%, -50%)'
  101. }
  102. e.preventDefault();
  103. },
  104. touchendLevitatedSphere(e) {
  105. localStorage.setItem('levitatedSpherePositionData', JSON.stringify(this.levitatedSpherePositionData))
  106. },
  107. // 点击悬浮球
  108. clickHandler(e){
  109. this.$emit('onClick');
  110. e.preventDefault();
  111. },
  112. }
  113. }
  114. </script>
  115. <style lang="scss" scoped>
  116. .float-btn-wrap{
  117. position: absolute;
  118. left: 15px;
  119. top: 15px;
  120. z-index: 1;
  121. .round-outside,
  122. .round-small{
  123. border-radius: 50%;
  124. }
  125. .round-outside{
  126. padding: 4px;
  127. background-color: #A5A5A5;
  128. // 设置透明度
  129. opacity: 0.8;
  130. .round-small{
  131. padding: 4px;
  132. width: 28px;
  133. height: 28px;
  134. background-color: #EDEDED;
  135. opacity: 0.8;
  136. .status{
  137. display: flex;
  138. justify-content: space-around;
  139. align-items: flex-end;
  140. width: 100%;
  141. height: 100%;
  142. padding-bottom: 4px;
  143. .short,
  144. .middle,
  145. .high{
  146. width: 4px;
  147. background-color: #A5A5A5;
  148. margin: 0 1px;
  149. }
  150. &.success{
  151. .short,
  152. .middle,
  153. .high{
  154. background-color: #44E2B1;
  155. }
  156. }
  157. &.warning{
  158. .short,
  159. .middle,
  160. .high{
  161. background-color: #FF9800;
  162. }
  163. }
  164. &.danger{
  165. .short,
  166. .middle,
  167. .high{
  168. background-color: #FF2627;
  169. }
  170. }
  171. .short{
  172. height: 40%;
  173. }
  174. .middle{
  175. height: 60%;
  176. }
  177. .high{
  178. height: 80%;
  179. }
  180. }
  181. }
  182. }
  183. }
  184. </style>