service.ts 2.5 KB

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