getCloseEdge.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { CloseEdgeEnum, ReachTypeEnum } from '../types';
  2. /**
  3. * 接触左边或右边边缘
  4. * @param x
  5. * @param scale
  6. * @param width
  7. * @return CloseEdgeEnum
  8. */
  9. export function getClosedHorizontal(
  10. x: number,
  11. scale: number,
  12. width: number,
  13. ): CloseEdgeEnum {
  14. const { innerWidth } = window;
  15. const currentWidth = width * scale;
  16. // 图片超出的宽度
  17. const outOffsetX = (currentWidth - innerWidth) / 2;
  18. if (currentWidth <= innerWidth) {
  19. return CloseEdgeEnum.Small;
  20. } else if (x > 0 && outOffsetX - x <= 0) {
  21. return CloseEdgeEnum.Before;
  22. } else if (x < 0 && outOffsetX + x <= 0) {
  23. return CloseEdgeEnum.After;
  24. }
  25. return CloseEdgeEnum.Normal;
  26. }
  27. /**
  28. * 接触上边或下边边缘
  29. * @param y
  30. * @param scale
  31. * @param height
  32. * @return CloseEdgeEnum
  33. */
  34. export function getClosedVertical(
  35. y: number,
  36. scale: number,
  37. height: number,
  38. ): CloseEdgeEnum {
  39. const { innerHeight } = window;
  40. const currentHeight = height * scale;
  41. // 图片超出的高度
  42. const outOffsetY = (currentHeight - innerHeight) / 2;
  43. if (currentHeight <= innerHeight) {
  44. return CloseEdgeEnum.Small;
  45. } else if (y > 0 && outOffsetY - y <= 0) {
  46. return CloseEdgeEnum.Before;
  47. } else if (y < 0 && outOffsetY + y <= 0) {
  48. return CloseEdgeEnum.After;
  49. }
  50. return CloseEdgeEnum.Normal;
  51. }
  52. /**
  53. * 获取接触边缘类型
  54. * @param horizontalCloseEdge
  55. * @param verticalCloseEdge
  56. * @param reachState
  57. */
  58. export function getReachType({
  59. horizontalCloseEdge,
  60. verticalCloseEdge,
  61. reachState,
  62. }: {
  63. horizontalCloseEdge: CloseEdgeEnum;
  64. verticalCloseEdge: CloseEdgeEnum;
  65. reachState: ReachTypeEnum;
  66. }): ReachTypeEnum {
  67. if (
  68. (horizontalCloseEdge && reachState === ReachTypeEnum.Normal) ||
  69. reachState === ReachTypeEnum.XReach
  70. ) {
  71. return ReachTypeEnum.XReach;
  72. } else if (
  73. (verticalCloseEdge && reachState === ReachTypeEnum.Normal) ||
  74. reachState === ReachTypeEnum.YReach
  75. ) {
  76. return ReachTypeEnum.YReach;
  77. }
  78. return ReachTypeEnum.Normal;
  79. }