u-floating.vue 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 lang="uts">
  8. type TouchItem = {
  9. clientX : number
  10. clientY : number
  11. }
  12. type TouchEvent = {
  13. touches : Array<TouchItem>
  14. }
  15. export default {
  16. name: 'FloatingButton',
  17. data() {
  18. return {
  19. top: 430,
  20. left: 20,
  21. startX: 0,
  22. startY: 0,
  23. dragging: false
  24. };
  25. },
  26. methods: {
  27. onTouchStart(event : TouchEvent) {
  28. this.startX = event.touches[0].clientX
  29. this.startY = event.touches[0].clientY
  30. this.dragging = true
  31. },
  32. onTouchMove(event : TouchEvent) {
  33. if (this.dragging) {
  34. const deltaX = event.touches[0].clientX - this.startX
  35. const deltaY = event.touches[0].clientY - this.startY
  36. this.startX = event.touches[0].clientX
  37. this.startY = event.touches[0].clientY
  38. this.top += deltaY
  39. this.left += deltaX
  40. }
  41. },
  42. onTouchEnd() {
  43. this.dragging = false
  44. }
  45. }
  46. }
  47. </script>
  48. <style scoped>
  49. .floating-button {
  50. position: fixed;
  51. z-index: 9999;
  52. background-color: #fff;
  53. padding: 20rpx; /* converted from 10px */
  54. /* use explicit half of width to create perfect circle in rpx */
  55. border-radius: 35rpx;
  56. width: 70rpx;
  57. height: 70rpx;
  58. display: flex;
  59. justify-content: center;
  60. align-items: center;
  61. box-shadow: 0 4rpx 10rpx rgba(0, 0, 0, 0.3);
  62. }
  63. </style>