slideToSuitableOffset.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { animationType } from '../types';
  2. import { defaultAnimationConfig } from '../variables';
  3. import slideToPosition from './slideToPosition';
  4. import { getClosedHorizontal, getClosedVertical } from './getCloseEdge';
  5. /**
  6. * 适应到合适的图片偏移量
  7. */
  8. const slideToSuitableOffset = ({
  9. x,
  10. y,
  11. lastX,
  12. lastY,
  13. width,
  14. height,
  15. scale,
  16. touchedTime,
  17. }: {
  18. x: number;
  19. y: number;
  20. lastX: number;
  21. lastY: number;
  22. width: number;
  23. height: number;
  24. scale: number;
  25. touchedTime: number;
  26. }): {
  27. x: number;
  28. y: number;
  29. } & animationType => {
  30. // 滑动到结果的位置
  31. const { endX, endY, animation } = slideToPosition({
  32. x,
  33. y,
  34. lastX,
  35. lastY,
  36. touchedTime,
  37. });
  38. let currentX = endX;
  39. let currentY = endY;
  40. const { innerWidth, innerHeight } = window;
  41. // 图片超出的长度
  42. const outOffsetX = (width * scale - innerWidth) / 2;
  43. const outOffsetY = (height * scale - innerHeight) / 2;
  44. const horizontalType = getClosedHorizontal(endX, scale, width);
  45. const verticalType = getClosedVertical(endY, scale, height);
  46. // x
  47. if (horizontalType === 1) {
  48. currentX = 0;
  49. } else if (horizontalType === 2) {
  50. currentX = outOffsetX;
  51. } else if (horizontalType === 3) {
  52. currentX = -outOffsetX;
  53. }
  54. // y
  55. if (verticalType === 1) {
  56. currentY = 0;
  57. } else if (verticalType === 2) {
  58. currentY = outOffsetY;
  59. } else if (verticalType === 3) {
  60. currentY = -outOffsetY;
  61. }
  62. const isClosedEdge = horizontalType !== 0 || verticalType !== 0;
  63. return {
  64. x: currentX,
  65. y: currentY,
  66. animation: isClosedEdge ? defaultAnimationConfig : animation,
  67. };
  68. };
  69. export default slideToSuitableOffset;