dragButton.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <view>
  3. <view
  4. id="_drag_button"
  5. class="drag"
  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. <slot></slot>
  14. </view>
  15. </view>
  16. </template>
  17. <script>
  18. export default {
  19. name: 'drag-button',
  20. props: {
  21. isDock:{
  22. type: Boolean,
  23. default: true
  24. },
  25. existTabBar:{
  26. type: Boolean,
  27. default: false
  28. }
  29. },
  30. data() {
  31. return {
  32. top:1000,
  33. left:1000,
  34. width: 0,
  35. height: 0,
  36. offsetWidth: 0,
  37. offsetHeight: 0,
  38. windowWidth: 0,
  39. windowHeight: 0,
  40. isMove: false,
  41. edge: 10,
  42. }
  43. },
  44. mounted() {
  45. const sys = uni.getSystemInfoSync();
  46. this.windowWidth = sys.windowWidth;
  47. this.windowHeight = sys.windowHeight;
  48. this.existTabBar && (this.windowHeight -= 80);
  49. if (sys.windowTop) {
  50. this.windowHeight += sys.windowTop;
  51. }
  52. // console.log(sys)
  53. const query = uni.createSelectorQuery().in(this);
  54. query.select('#_drag_button').boundingClientRect(data => {
  55. this.width = data.width;
  56. this.height = data.height;
  57. this.offsetWidth = data.width / 2;
  58. this.offsetHeight = data.height / 2;
  59. this.left = this.windowWidth - this.width+4;
  60. this.top = this.windowHeight - this.height - this.edge;
  61. }).exec();
  62. },
  63. methods: {
  64. click() {
  65. this.$emit('btnClick');
  66. },
  67. touchstart(e) {
  68. this.$emit('btnTouchstart');
  69. },
  70. touchmove(e) {
  71. // 单指触摸
  72. if (e.touches.length !== 1) {
  73. return false;
  74. }
  75. this.isMove = true;
  76. this.left = e.touches[0].clientX - this.offsetWidth;
  77. let clientY = e.touches[0].clientY - this.offsetHeight;
  78. // #ifdef H5
  79. clientY += this.height;
  80. // #endif
  81. let edgeBottom = this.windowHeight - this.height - this.edge;
  82. // 上下触及边界
  83. if (clientY < this.edge) {
  84. this.top = this.edge;
  85. } else if (clientY > edgeBottom) {
  86. this.top = edgeBottom;
  87. } else {
  88. this.top = clientY
  89. }
  90. },
  91. touchend(e) {
  92. if (this.isDock) {
  93. let edgeRigth = this.windowWidth - this.width;
  94. if (this.left < this.windowWidth / 2 - this.offsetWidth) {
  95. this.left = -4;
  96. } else {
  97. this.left = edgeRigth+4;
  98. }
  99. }
  100. this.isMove = false;
  101. this.$emit('btnTouchend');
  102. },
  103. }
  104. }
  105. </script>
  106. <style lang="scss">
  107. .drag {
  108. display: flex;
  109. justify-content: center;
  110. align-items: center;
  111. // background-color: rgba(0, 0, 0, 0.5);
  112. // box-shadow: 0 0 6upx rgba(0, 0, 0, 0.4);
  113. // color: $uni-text-color-inverse;
  114. min-width: 80upx;
  115. min-height: 80upx;
  116. // border-radius: 50%;
  117. // font-size: $uni-font-size-sm;
  118. position: fixed;
  119. z-index: 99;
  120. &.transition {
  121. transition: left .3s ease,top .3s ease;
  122. }
  123. }
  124. </style>