float-btn.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <template>
  2. <view class="floatBtn" ref="floatBtnRef" @click="showChargingOrder()" :class="[{moveBtn: longClick}, `${btnType}Btn`]">
  3. <div class="lighting"></div>
  4. <span style="margin-right:16rpx;">充电中</span>
  5. </view>
  6. </template>
  7. <script>
  8. export default {
  9. data() {
  10. return {
  11. timeOutEvent: 0,
  12. longClick: 0,
  13. // 手指原始位置
  14. oldMousePos: {},
  15. // 元素原始位置
  16. oldNodePos: {},
  17. btnType: 'right'
  18. };
  19. },
  20. props: {
  21. connectorId: {
  22. default: null,
  23. type: String,
  24. },
  25. },
  26. mounted() {
  27. },
  28. onShow() {
  29. },
  30. methods: {
  31. showChargingOrder() {
  32. if (this.connectorId != null) {
  33. uni.navigateTo({
  34. url: `/pages/station/charge/charging?id=${this.connectorId}`
  35. })
  36. }
  37. },
  38. touchstart(ev) {
  39. // 定时器控制长按时间,超过500毫秒开始进行拖拽
  40. this.timeOutEvent = setTimeout(() => {
  41. this.longClick = 1;
  42. }, 500);
  43. let selectDom = ev.currentTarget;
  44. const { pageX, pageY } = ev.touches[0];
  45. const { offsetLeft, offsetTop } = selectDom;
  46. // 手指原始位置
  47. this.oldMousePos = {
  48. x: pageX,
  49. y: pageY
  50. };
  51. // 元素原始位置
  52. this.oldNodePos = {
  53. x: offsetLeft,
  54. y: offsetTop
  55. };
  56. this.$refs.floatBtn.style.left = `${offsetLeft}px`;
  57. this.$refs.floatBtn.style.top = `${offsetTop}px`;
  58. },
  59. touchMove(ev) {
  60. // 未达到500毫秒就移动则不触发长按,清空定时器
  61. clearTimeout(this.timeOutEvent);
  62. if (this.longClick === 1) {
  63. const selectDom = ev.currentTarget;
  64. // x轴偏移量
  65. const lefts = this.oldMousePos.x - this.oldNodePos.x;
  66. // y轴偏移量
  67. const tops = this.oldMousePos.y - this.oldNodePos.y;
  68. const { pageX, pageY } = ev.touches[0]; // 手指位置
  69. selectDom.style.left = `${pageX - lefts}px`;
  70. selectDom.style.top = `${pageY - tops}px`;
  71. }
  72. },
  73. touchEnd(ev) {
  74. // 清空定时器
  75. clearTimeout(this.timeOutEvent);
  76. if (this.longClick === 1) {
  77. this.longClick = 0;
  78. let selectDom = ev.currentTarget;
  79. const { clientWidth, clientHeight } = document.body;
  80. const { offsetLeft, offsetTop } = selectDom;
  81. selectDom.style.left =
  82. (offsetLeft + 50) > (clientWidth / 2) ?
  83. 'calc(100% - 196px)' : 0;
  84. if (offsetTop < 90) {
  85. selectDom.style.top = '90px';
  86. } else if (offsetTop + 36 > clientHeight) {
  87. selectDom.style.top = `${clientHeight - 36}px`;
  88. }
  89. this.btnType =
  90. (offsetLeft + 50) > (clientWidth / 2) ?
  91. 'right' : 'left';
  92. }
  93. },
  94. },
  95. };
  96. </script>
  97. <style scoped lang="scss">
  98. @import "./float-btn.scss";
  99. </style>