position.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.position = exports.height = exports.width = exports.offset = void 0;
  4. const css_1 = require("./css");
  5. const elem_1 = require("./elem");
  6. function offset(elem) {
  7. const rect = elem.getBoundingClientRect();
  8. const win = elem.ownerDocument.defaultView;
  9. return {
  10. top: rect.top + win.pageYOffset,
  11. left: rect.left + win.pageXOffset,
  12. };
  13. }
  14. exports.offset = offset;
  15. function width(elem) {
  16. const rect = elem.getBoundingClientRect();
  17. return rect.width;
  18. }
  19. exports.width = width;
  20. function height(elem) {
  21. const rect = elem.getBoundingClientRect();
  22. return rect.height;
  23. }
  24. exports.height = height;
  25. function position(elem) {
  26. const isFixed = (0, css_1.computeStyle)(elem, 'position') === 'fixed';
  27. let offsetValue;
  28. if (isFixed) {
  29. const rect = elem.getBoundingClientRect();
  30. offsetValue = { left: rect.left, top: rect.top };
  31. }
  32. else {
  33. offsetValue = offset(elem);
  34. }
  35. if (!isFixed) {
  36. const doc = elem.ownerDocument;
  37. let offsetParent = elem.offsetParent || doc.documentElement;
  38. while ((offsetParent === doc.body || offsetParent === doc.documentElement) &&
  39. (0, css_1.computeStyle)(offsetParent, 'position') === 'static') {
  40. offsetParent = offsetParent.parentNode;
  41. }
  42. if (offsetParent !== elem && (0, elem_1.isElement)(offsetParent)) {
  43. const parentOffset = offset(offsetParent);
  44. offsetValue.top -=
  45. parentOffset.top + (0, css_1.computeStyleInt)(offsetParent, 'borderTopWidth');
  46. offsetValue.left -=
  47. parentOffset.left + (0, css_1.computeStyleInt)(offsetParent, 'borderLeftWidth');
  48. }
  49. }
  50. return {
  51. top: offsetValue.top - (0, css_1.computeStyleInt)(elem, 'marginTop'),
  52. left: offsetValue.left - (0, css_1.computeStyleInt)(elem, 'marginLeft'),
  53. };
  54. }
  55. exports.position = position;
  56. //# sourceMappingURL=position.js.map