You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
2.9 KiB

6 months ago
  1. <template>
  2. <view>
  3. <view
  4. id="_drag_button"
  5. class="drag"
  6. :style="'left: ' + left + 'px; top:' + top + 'px;'"
  7. @touchstart="touchstart"
  8. @touchmove.stop.prevent="touchmove"
  9. @touchend="touchend"
  10. @click.stop.prevent="click"
  11. :class="{transition: isDock && !isMove }"
  12. >
  13. <text>{{ text }}</text>
  14. </view>
  15. </view>
  16. </template>
  17. <script>
  18. export default {
  19. name: 'drag-button',
  20. props: {
  21. isDock:{
  22. type: Boolean,
  23. default: false
  24. },
  25. existTabBar:{
  26. type: Boolean,
  27. default: false
  28. }
  29. },
  30. data() {
  31. return {
  32. top:0,
  33. left:0,
  34. width: 0,
  35. height: 0,
  36. offsetWidth: 0,
  37. offsetHeight: 0,
  38. windowWidth: 0,
  39. windowHeight: 0,
  40. isMove: true,
  41. edge: 10,
  42. text: '客服'
  43. }
  44. },
  45. mounted() {
  46. const sys = uni.getSystemInfoSync();
  47. this.windowWidth = sys.windowWidth;
  48. this.windowHeight = sys.windowHeight;
  49. // #ifdef APP-PLUS
  50. this.existTabBar && (this.windowHeight -= 50);
  51. // #endif
  52. if (sys.windowTop) {
  53. this.windowHeight += sys.windowTop;
  54. }
  55. console.log(sys)
  56. const query = uni.createSelectorQuery().in(this);
  57. query.select('#_drag_button').boundingClientRect(data => {
  58. this.width = data.width;
  59. this.height = data.height;
  60. this.offsetWidth = data.width / 2;
  61. this.offsetHeight = data.height / 2;
  62. this.left = this.windowWidth - this.width - this.edge;
  63. this.top = this.windowHeight - this.height - this.edge;
  64. }).exec();
  65. },
  66. methods: {
  67. click() {
  68. this.$emit('btnClick');
  69. },
  70. touchstart(e) {
  71. this.$emit('btnTouchstart');
  72. },
  73. touchmove(e) {
  74. // 单指触摸
  75. if (e.touches.length !== 1) {
  76. return false;
  77. }
  78. this.isMove = true;
  79. this.left = e.touches[0].clientX - this.offsetWidth;
  80. let clientY = e.touches[0].clientY - this.offsetHeight;
  81. // #ifdef H5
  82. clientY += this.height;
  83. // #endif
  84. let edgeBottom = this.windowHeight - this.height - this.edge;
  85. // 上下触及边界
  86. if (clientY < this.edge) {
  87. this.top = this.edge;
  88. } else if (clientY > edgeBottom) {
  89. this.top = edgeBottom;
  90. } else {
  91. this.top = clientY
  92. }
  93. },
  94. touchend(e) {
  95. if (this.isDock) {
  96. let edgeRigth = this.windowWidth - this.width - this.edge;
  97. if (this.left < this.windowWidth / 2 - this.offsetWidth) {
  98. this.left = this.edge;
  99. } else {
  100. this.left = edgeRigth;
  101. }
  102. }
  103. this.isMove = false;
  104. this.$emit('btnTouchend');
  105. },
  106. }}
  107. </script>
  108. <style lang="scss">
  109. .drag {
  110. display: flex;
  111. justify-content: center;
  112. align-items: center;
  113. background-color: #46B98E;
  114. box-shadow: 0 0 6upx rgba(0, 0, 0, 0.4);
  115. color: $uni-text-color-inverse;
  116. width: 80upx;
  117. height: 80upx;
  118. border-radius: 50%;
  119. font-size: $uni-font-size-sm;
  120. position: fixed;
  121. z-index: 999999;
  122. &.transition {
  123. transition: left .3s ease,top .3s ease;
  124. }
  125. }
  126. </style>