u-floating.vue 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <template>
  2. <view class="floating-button" :style="{ top: top + 'px', left: left + 'px' }" @touchstart="onTouchStart"
  3. @touchmove="onTouchMove" @touchend="onTouchEnd">
  4. <text class="textIcon icon-jingwuicon_svg-" style="font-size: 60rpx;color: #FF4D4D;"></text>
  5. </view>
  6. </template>
  7. <script>
  8. export default {
  9. name: 'FloatingButton',
  10. data() {
  11. return {
  12. top: 430,
  13. left: 20,
  14. startX: 0,
  15. startY: 0,
  16. dragging: false
  17. };
  18. },
  19. methods: {
  20. onTouchStart(event) {
  21. this.startX = event.touches[0].clientX;
  22. this.startY = event.touches[0].clientY;
  23. this.dragging = true;
  24. },
  25. onTouchMove(event) {
  26. if (this.dragging) {
  27. const deltaX = event.touches[0].clientX - this.startX;
  28. const deltaY = event.touches[0].clientY - this.startY;
  29. this.startX = event.touches[0].clientX;
  30. this.startY = event.touches[0].clientY;
  31. this.top += deltaY;
  32. this.left += deltaX;
  33. }
  34. },
  35. onTouchEnd() {
  36. this.dragging = false;
  37. }
  38. }
  39. };
  40. </script>
  41. <style scoped>
  42. .floating-button {
  43. position: fixed;
  44. z-index: 9999;
  45. background-color: #fff;
  46. padding: 10px;
  47. border-radius: 50%;
  48. width: 70rpx;
  49. height: 70rpx;
  50. display: flex;
  51. justify-content: center;
  52. align-items: center;
  53. box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
  54. }
  55. </style>