service.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // components/service/service.ts
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. // properties: {
  7. // popShow: {
  8. // type: Boolean,
  9. // default: false
  10. // }
  11. // },
  12. /**
  13. * 组件的初始数据
  14. */
  15. data: {
  16. popShow: false,
  17. top: 0, // 初始的上偏移
  18. startX: 0, // 触摸起始点 X 坐标
  19. startY: 0, // 触摸起始点 Y 坐标
  20. windowWidth: 0, // 屏幕宽度
  21. windowHeight: 0, // 屏幕高度
  22. elementHeight: 60 // 元素高度
  23. },
  24. attached() {
  25. // 获取屏幕宽高
  26. const systemInfo = wx.getWindowInfo();
  27. this.setData({
  28. windowWidth: systemInfo.windowWidth,
  29. windowHeight: systemInfo.windowHeight,
  30. top: systemInfo.windowHeight - 180
  31. });
  32. },
  33. /**
  34. * 组件的方法列表
  35. */
  36. methods: {
  37. onClose() {
  38. // this.triggerEvent('changePop', false)
  39. this.setData({
  40. popShow: false
  41. })
  42. },
  43. onOpen() {
  44. //
  45. },
  46. onShow() {
  47. this.setData({
  48. popShow: true
  49. })
  50. },
  51. onTouchStart(e: any) {
  52. // 记录触摸起始点的 Y 坐标
  53. this.setData({
  54. startY: e.touches[0].clientY
  55. });
  56. },
  57. onTouchMove(e: any) {
  58. // 计算上下方向的偏移量
  59. const deltaY = e.touches[0].clientY - this.data.startY;
  60. // 更新元素的垂直位置
  61. this.setData({
  62. top: this.data.top + deltaY,
  63. startY: e.touches[0].clientY
  64. });
  65. },
  66. onTouchEnd() {
  67. const { top, windowHeight, elementHeight } = this.data;
  68. // 计算与顶部和底部边界的距离
  69. const distanceTop = top;
  70. const moveHeight = top + elementHeight
  71. // 判断元素与顶部和底部的距离,选择最近的边界
  72. if (distanceTop < 0) {
  73. this.setData({ top: 0 }); // 吸附到顶部
  74. } else if(moveHeight >= windowHeight) {
  75. this.setData({ top: windowHeight - elementHeight })
  76. } else {
  77. this.setData({ top })
  78. }
  79. }
  80. }
  81. })