position.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. require('../browser.js');
  4. var core = require('@vueuse/core');
  5. const isInContainer = (el, container) => {
  6. if (!core.isClient || !el || !container)
  7. return false;
  8. const elRect = el.getBoundingClientRect();
  9. let containerRect;
  10. if (container instanceof Element) {
  11. containerRect = container.getBoundingClientRect();
  12. } else {
  13. containerRect = {
  14. top: 0,
  15. right: window.innerWidth,
  16. bottom: window.innerHeight,
  17. left: 0
  18. };
  19. }
  20. return elRect.top < containerRect.bottom && elRect.bottom > containerRect.top && elRect.right > containerRect.left && elRect.left < containerRect.right;
  21. };
  22. const getOffsetTop = (el) => {
  23. let offset = 0;
  24. let parent = el;
  25. while (parent) {
  26. offset += parent.offsetTop;
  27. parent = parent.offsetParent;
  28. }
  29. return offset;
  30. };
  31. const getOffsetTopDistance = (el, containerEl) => {
  32. return Math.abs(getOffsetTop(el) - getOffsetTop(containerEl));
  33. };
  34. const getClientXY = (event) => {
  35. let clientX;
  36. let clientY;
  37. if (event.type === "touchend") {
  38. clientY = event.changedTouches[0].clientY;
  39. clientX = event.changedTouches[0].clientX;
  40. } else if (event.type.startsWith("touch")) {
  41. clientY = event.touches[0].clientY;
  42. clientX = event.touches[0].clientX;
  43. } else {
  44. clientY = event.clientY;
  45. clientX = event.clientX;
  46. }
  47. return {
  48. clientX,
  49. clientY
  50. };
  51. };
  52. exports.getClientXY = getClientXY;
  53. exports.getOffsetTop = getOffsetTop;
  54. exports.getOffsetTopDistance = getOffsetTopDistance;
  55. exports.isInContainer = isInContainer;
  56. //# sourceMappingURL=position.js.map