cm-drag.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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='https://static.caimei365.com/app/img/icon/icon-cart-active@3x.png' mode="widthFix"></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. top:300,
  40. left:150,
  41. width: 0,
  42. height: 0,
  43. offsetWidth: 0,
  44. offsetHeight: 0,
  45. windowWidth: 0,
  46. windowHeight: 0,
  47. isMove: true,
  48. edge: 10,
  49. }
  50. },
  51. mounted() {
  52. const sys = uni.getSystemInfoSync();
  53. this.windowWidth = sys.windowWidth;
  54. this.windowHeight = sys.windowHeight;
  55. // #ifdef APP-PLUS
  56. this.existTabBar && (this.windowHeight -= 50);
  57. // #endif
  58. if (sys.windowTop) {
  59. this.windowHeight += sys.windowTop;
  60. }
  61. // console.log(sys)
  62. const query = uni.createSelectorQuery().in(this);
  63. query.select('#_drag_button').boundingClientRect(data => {
  64. this.width = data.width;
  65. this.height = data.height;
  66. this.offsetWidth = data.width / 2;
  67. this.offsetHeight = data.height / 2;
  68. this.left = this.windowWidth - this.width - this.edge-5;
  69. this.top = this.windowHeight - this.height - this.edge-150;
  70. }).exec();
  71. },
  72. methods: {
  73. click() {
  74. this.$emit('btnClick');
  75. },
  76. touchstart(e) {
  77. this.$emit('btnTouchstart');
  78. },
  79. touchmove(e) {
  80. // 单指触摸
  81. if (e.touches.length !== 1) {
  82. return false;
  83. }
  84. this.isMove = true;
  85. this.left = e.touches[0].clientX - this.offsetWidth;
  86. let clientY = e.touches[0].clientY - this.offsetHeight;
  87. // #ifdef H5
  88. clientY += this.height;
  89. // #endif
  90. let edgeBottom = this.windowHeight - this.height - this.edge;
  91. // 上下触及边界
  92. if (clientY < this.edge) {
  93. this.top = this.edge;
  94. } else if (clientY > edgeBottom) {
  95. this.top = edgeBottom;
  96. } else {
  97. this.top = clientY
  98. }
  99. },
  100. touchend(e) {
  101. if (this.isDock) {
  102. let edgeRigth = this.windowWidth - this.width - this.edge;
  103. if (this.left < this.windowWidth / 2 - this.offsetWidth) {
  104. this.left = this.edge;
  105. } else {
  106. this.left = edgeRigth;
  107. }
  108. }
  109. this.isMove = false;
  110. this.$emit('btnTouchend');
  111. },
  112. }}
  113. </script>
  114. <style lang="scss">
  115. .cart-icon {
  116. width: 92rpx;
  117. height: 92rpx;
  118. border-radius: 50%;
  119. background: rgba(255, 147, 0, 0.5);
  120. position: fixed;
  121. right: 24rpx;
  122. bottom: 28%;
  123. display: flex;
  124. align-items: center;
  125. justify-content: center;
  126. cursor: pointer;
  127. z-index: 20;
  128. image {
  129. width: 58rpx;
  130. height: 58rpx;
  131. }
  132. text {
  133. font-size: 28rpx;
  134. position: absolute;
  135. top: -10rpx;
  136. right: 0;
  137. }
  138. &.transition {
  139. transition: left .3s ease,top .3s ease;
  140. }
  141. }
  142. </style>