DragButton.vue 3.3 KB

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