cm-drag.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <template>
  2. <view>
  3. <view
  4. id="_drag_button"
  5. class="cart-icon"
  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 v-if="cartNum > 0" class="uni-badge uni-badge-error uni-small uni-badge--small icon-num">
  14. {{ cartNum >= 100 ? '99+' : cartNum }}
  15. </text>
  16. <image :src="staticUrl + 'icon-cart-active@3x.png'" mode=""></image>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. export default {
  22. name: 'drag-button',
  23. props: {
  24. cartNum: {
  25. type: Number,
  26. default: 0
  27. },
  28. isDock: {
  29. type: Boolean,
  30. default: false
  31. },
  32. existTabBar: {
  33. type: Boolean,
  34. default: false
  35. }
  36. },
  37. data() {
  38. return {
  39. staticUrl: this.global.staticUrl,
  40. top: 300,
  41. left: 150,
  42. width: 0,
  43. height: 0,
  44. offsetWidth: 0,
  45. offsetHeight: 0,
  46. windowWidth: 0,
  47. windowHeight: 0,
  48. isMove: true,
  49. edge: 10
  50. }
  51. },
  52. mounted() {
  53. const sys = uni.getSystemInfoSync()
  54. this.windowWidth = sys.windowWidth
  55. this.windowHeight = sys.windowHeight
  56. // #ifdef APP-PLUS
  57. this.existTabBar && (this.windowHeight -= 50)
  58. // #endif
  59. if (sys.windowTop) {
  60. this.windowHeight += sys.windowTop
  61. }
  62. // console.log(sys)
  63. const query = uni.createSelectorQuery().in(this)
  64. query
  65. .select('#_drag_button')
  66. .boundingClientRect(data => {
  67. this.width = data.width
  68. this.height = data.height
  69. this.offsetWidth = data.width / 2
  70. this.offsetHeight = data.height / 2
  71. this.left = this.windowWidth - this.width - this.edge - 5
  72. this.top = this.windowHeight - this.height - this.edge - 150
  73. })
  74. .exec()
  75. },
  76. methods: {
  77. click() {
  78. this.$emit('btnClick')
  79. },
  80. touchstart(e) {
  81. this.$emit('btnTouchstart')
  82. },
  83. touchmove(e) {
  84. // 单指触摸
  85. if (e.touches.length !== 1) {
  86. return false
  87. }
  88. this.isMove = true
  89. this.left = e.touches[0].clientX - this.offsetWidth
  90. let clientY = e.touches[0].clientY - this.offsetHeight
  91. // #ifdef H5
  92. clientY += this.height
  93. // #endif
  94. let edgeBottom = this.windowHeight - this.height - this.edge
  95. // 上下触及边界
  96. if (clientY < this.edge) {
  97. this.top = this.edge
  98. } else if (clientY > edgeBottom) {
  99. this.top = edgeBottom
  100. } else {
  101. this.top = clientY
  102. }
  103. },
  104. touchend(e) {
  105. if (this.isDock) {
  106. let edgeRigth = this.windowWidth - this.width - this.edge
  107. if (this.left < this.windowWidth / 2 - this.offsetWidth) {
  108. this.left = this.edge
  109. } else {
  110. this.left = edgeRigth
  111. }
  112. }
  113. this.isMove = false
  114. this.$emit('btnTouchend')
  115. }
  116. }
  117. }
  118. </script>
  119. <style lang="scss">
  120. .cart-icon {
  121. width: 92rpx;
  122. height: 92rpx;
  123. border-radius: 50%;
  124. background: rgba(0,0,0,0.3);
  125. position: fixed;
  126. right: 24rpx;
  127. bottom: 28%;
  128. display: flex;
  129. align-items: center;
  130. justify-content: center;
  131. cursor: pointer;
  132. z-index: 20;
  133. image {
  134. width: 58rpx;
  135. height: 58rpx;
  136. }
  137. text {
  138. font-size: 28rpx;
  139. position: absolute;
  140. top: -10rpx;
  141. right: 0;
  142. }
  143. &.transition {
  144. transition: left 0.3s ease, top 0.3s ease;
  145. }
  146. }
  147. </style>