drag-button.vue 2.9 KB

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