cm-drag.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. <text class="iconfont icon-gouwuche-mianxing"></text>
  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.$Static,
  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. created() {
  53. console.log(this.cartNum)
  54. },
  55. mounted() {
  56. const sys = uni.getSystemInfoSync();
  57. this.windowWidth = sys.windowWidth;
  58. this.windowHeight = sys.windowHeight;
  59. // #ifdef APP-PLUS
  60. this.existTabBar && (this.windowHeight -= 50);
  61. // #endif
  62. if (sys.windowTop) {
  63. this.windowHeight += sys.windowTop;
  64. }
  65. // console.log(sys)
  66. const query = uni.createSelectorQuery().in(this);
  67. query.select('#_drag_button').boundingClientRect(data => {
  68. this.width = data.width;
  69. this.height = data.height;
  70. this.offsetWidth = data.width / 2;
  71. this.offsetHeight = data.height / 2;
  72. this.left = this.windowWidth - this.width - this.edge-5;
  73. this.top = this.windowHeight - this.height - this.edge-150;
  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. </script>
  118. <style lang="scss">
  119. .cart-icon {
  120. width: 92rpx;
  121. height: 92rpx;
  122. border-radius: 50%;
  123. background: rgba(0,0,0, 0.2);
  124. position: fixed;
  125. right: 24rpx;
  126. bottom: 28%;
  127. display: flex;
  128. align-items: center;
  129. justify-content: center;
  130. cursor: pointer;
  131. z-index: 20;
  132. line-height: 92rpx;
  133. text-align: center;
  134. .icon-gouwuche-mianxing {
  135. font-size: 56rpx;
  136. color: #ff457b;
  137. }
  138. .uni-badge {
  139. font-size: 28rpx;
  140. position: absolute;
  141. top: -10rpx;
  142. right: 0;
  143. }
  144. &.transition {
  145. transition: left .3s ease,top .3s ease;
  146. }
  147. }
  148. </style>