position.js 1.43 KB
Newer Older
Sangjune Bae's avatar
Sangjune Bae committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import _extends from "@babel/runtime/helpers/esm/extends";
import css from './css';
import getOffset from './offset';
import getOffsetParent from './offsetParent';
import scrollLeft from './scrollLeft';
import scrollTop from './scrollTop';

var nodeName = function nodeName(node) {
  return node.nodeName && node.nodeName.toLowerCase();
};

export default function position(node, offsetParent) {
  var parentOffset = {
    top: 0,
    left: 0
  };
  var offset; // Fixed elements are offset from window (parentOffset = {top:0, left: 0},
  // because it is its only offset parent

  if (css(node, 'position') === 'fixed') {
    offset = node.getBoundingClientRect();
  } else {
    var parent = offsetParent || getOffsetParent(node);
    offset = getOffset(node);
    if (nodeName(parent) !== 'html') parentOffset = getOffset(parent);
    var borderTop = String(css(parent, 'borderTopWidth') || 0);
    parentOffset.top += parseInt(borderTop, 10) - scrollTop(parent) || 0;
    var borderLeft = String(css(parent, 'borderLeftWidth') || 0);
    parentOffset.left += parseInt(borderLeft, 10) - scrollLeft(parent) || 0;
  }

  var marginTop = String(css(node, 'marginTop') || 0);
  var marginLeft = String(css(node, 'marginLeft') || 0); // Subtract parent offsets and node margins

  return _extends({}, offset, {
    top: offset.top - parentOffset.top - (parseInt(marginTop, 10) || 0),
    left: offset.left - parentOffset.left - (parseInt(marginLeft, 10) || 0)
  });
}