serviceWidget.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <template>
  2. <view>
  3. <view id="_drag_button" class="drag" :style="'left: ' + left + 'px; top:' + top + 'px;'"
  4. @touchmove.stop.prevent="touchmove" @touchend="touchend" @click.stop.prevent="click"
  5. :class="{transition: isDock && !isMove }">
  6. <image src="/static/img/customerService.png" mode="" style="width: 46px;height: 56px;"></image>
  7. </view>
  8. </view>
  9. </template>
  10. <script>
  11. export default {
  12. name: 'service-widget',
  13. props: {
  14. isDock: {
  15. type: Boolean,
  16. default: true
  17. },
  18. existTabBar: {
  19. type: Boolean,
  20. default: true
  21. },
  22. bottomPx: {
  23. type: Number,
  24. default: 120
  25. }
  26. },
  27. data() {
  28. return {
  29. top: 0,
  30. left: 0,
  31. width: 0,
  32. height: 0,
  33. offsetWidth: 0,
  34. offsetHeight: 0,
  35. windowWidth: 0,
  36. windowHeight: 0,
  37. isMove: true,
  38. edge: 10,
  39. text: '按钮'
  40. }
  41. },
  42. mounted() {
  43. const sys = uni.getSystemInfoSync();
  44. this.windowWidth = sys.windowWidth;
  45. this.windowHeight = sys.windowHeight;
  46. // #ifdef APP-PLUS
  47. this.existTabBar && (this.windowHeight -= 50);
  48. // #endif
  49. if (sys.windowTop) {
  50. this.windowHeight += sys.windowTop;
  51. }
  52. const query = uni.createSelectorQuery().in(this);
  53. query.select('#_drag_button').boundingClientRect(data => {
  54. this.width = data.width;
  55. this.height = data.height;
  56. this.offsetWidth = data.width / 2;
  57. this.offsetHeight = data.height / 2;
  58. this.left = this.windowWidth - this.width - this.edge;
  59. if (this.bottomPx * 1 > 0) {
  60. this.top = this.windowHeight - this.height - this.bottomPx;
  61. } else {
  62. this.top = this.windowHeight - this.height - this.edge;
  63. }
  64. }).exec();
  65. //避免部分情况下图片加载延迟导致高度渲染出错
  66. setTimeout(() => {
  67. const query = uni.createSelectorQuery().in(this);
  68. query.select('#_drag_button').boundingClientRect(data => {
  69. this.width = data.width;
  70. this.height = data.height;
  71. this.offsetWidth = data.width / 2;
  72. this.offsetHeight = data.height / 2;
  73. this.left = this.windowWidth - this.width - this.edge;
  74. if (this.bottomPx * 1 > 0) {
  75. this.top = this.windowHeight - this.height - this.bottomPx;
  76. } else {
  77. this.top = this.windowHeight - this.height - this.edge;
  78. }
  79. }).exec();
  80. }, 200)
  81. },
  82. methods: {
  83. click() {
  84. this.$emit('btnClick');
  85. },
  86. // touchstart(e) {
  87. // this.$emit('btnTouchstart');
  88. // },
  89. touchmove(e) {
  90. // 单指触摸
  91. if (e.touches.length !== 1) {
  92. return false;
  93. }
  94. if (!this.isMove) {
  95. this.$emit('btnTouchstart');
  96. }
  97. this.isMove = true;
  98. this.left = e.touches[0].clientX - this.offsetWidth;
  99. let clientY = e.touches[0].clientY - this.offsetHeight;
  100. // #ifdef H5
  101. clientY += this.height;
  102. // #endif
  103. let edgeBottom = this.windowHeight - this.height - this.edge;
  104. // 上下触及边界
  105. if (clientY < this.edge) {
  106. this.top = this.edge;
  107. } else if (clientY > edgeBottom) {
  108. this.top = edgeBottom;
  109. } else {
  110. this.top = clientY
  111. }
  112. },
  113. touchend(e) {
  114. if (this.isDock) {
  115. let edgeRigth = this.windowWidth - this.width - this.edge;
  116. if (this.left < this.windowWidth / 2 - this.offsetWidth) {
  117. this.left = this.edge;
  118. } else {
  119. this.left = edgeRigth;
  120. }
  121. }
  122. if (this.isMove) {
  123. this.isMove = false;
  124. this.$emit('btnTouchend');
  125. }
  126. },
  127. }
  128. }
  129. </script>
  130. <style lang="scss">
  131. .drag {
  132. display: flex;
  133. justify-content: center;
  134. align-items: center;
  135. // background-color: rgba(0, 0, 0, 0.5);
  136. // box-shadow: 0 0 6upx rgba(0, 0, 0, 0.4);
  137. // color: $uni-text-color-inverse;
  138. // width: 80upx;
  139. // height: 80upx;
  140. min-width: 80rpx;
  141. min-height: 80rpx;
  142. border-radius: 50%;
  143. // font-size: $uni-font-size-sm;
  144. position: fixed;
  145. z-index: 999999;
  146. &.transition {
  147. transition: left .3s ease, top .3s ease;
  148. }
  149. }
  150. </style>