getCloseEdge.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /**
  2. * 接触左边或右边边缘
  3. * @param x
  4. * @param scale
  5. * @param width
  6. * @return 0. 未超出 1. 小于屏幕宽度 2. 接触左边 3. 接触右边
  7. */
  8. export const getClosedHorizontal = (
  9. x: number,
  10. scale: number,
  11. width: number,
  12. ): number => {
  13. const { innerWidth } = window;
  14. const currentWidth = width * scale;
  15. // 图片超出的宽度
  16. const outOffsetX = (currentWidth - innerWidth) / 2;
  17. if (currentWidth <= innerWidth) {
  18. return 1;
  19. } else if (x > 0 && outOffsetX - x <= 0) {
  20. return 2;
  21. } else if (x < 0 && outOffsetX + x <= 0) {
  22. return 3;
  23. }
  24. return 0;
  25. };
  26. /**
  27. * 接触上边或下边边缘
  28. * @param y
  29. * @param scale
  30. * @param height
  31. * @return 0. 未超出 1. 小于屏幕高度 2. 接触上边 3. 接触下边
  32. */
  33. export const getClosedVertical = (
  34. y: number,
  35. scale: number,
  36. height: number,
  37. ): number => {
  38. const { innerHeight } = window;
  39. const currentHeight = height * scale;
  40. // 图片超出的高度
  41. const outOffsetY = (currentHeight - innerHeight) / 2;
  42. if (currentHeight <= innerHeight) {
  43. return 1;
  44. } else if (y > 0 && outOffsetY - y <= 0) {
  45. return 2;
  46. } else if (y < 0 && outOffsetY + y <= 0) {
  47. return 3;
  48. }
  49. return 0;
  50. };