1 |
|
2 | var DOCUMENT_NODE = 9
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | function isDocument(d) {
|
13 | return d && d.nodeType === DOCUMENT_NODE
|
14 | }
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | function getDocument(node) {
|
27 | if (isDocument(node)) {
|
28 | return node
|
29 | } else if (isDocument(node.ownerDocument)) {
|
30 | return node.ownerDocument
|
31 | } else if (isDocument(node.document)) {
|
32 | return node.document
|
33 | } else if (node.parentNode) {
|
34 | return getDocument(node.parentNode)
|
35 |
|
36 |
|
37 | } else if (node.commonAncestorContainer) {
|
38 | return getDocument(node.commonAncestorContainer)
|
39 | } else if (node.startContainer) {
|
40 | return getDocument(node.startContainer)
|
41 |
|
42 |
|
43 | } else if (node.anchorNode) {
|
44 | return getDocument(node.anchorNode)
|
45 | }
|
46 | }
|
47 |
|
48 | function withinElement(child, parent) {
|
49 |
|
50 | if (!child) return false
|
51 |
|
52 |
|
53 | if (child.commonAncestorContainer) child = child.commonAncestorContainer
|
54 | else if (child.endContainer) child = child.endContainer
|
55 |
|
56 |
|
57 | if (child === window) return true
|
58 |
|
59 | return parent.contains(child)
|
60 | }
|
61 |
|
62 | module.exports = function offset(el) {
|
63 | var doc = getDocument(el)
|
64 | if (!doc) return
|
65 |
|
66 |
|
67 | if (!withinElement(el, doc)) return
|
68 |
|
69 | var body = doc.body
|
70 | if (body === el) {
|
71 | return bodyOffset(el)
|
72 | }
|
73 |
|
74 | var box = { top: 0, left: 0 }
|
75 | if (typeof el.getBoundingClientRect !== "undefined") {
|
76 |
|
77 |
|
78 | box = el.getBoundingClientRect()
|
79 |
|
80 | if (el.collapsed && box.left === 0 && box.top === 0) {
|
81 |
|
82 |
|
83 | var span = doc.createElement("span")
|
84 |
|
85 |
|
86 |
|
87 | span.appendChild(doc.createTextNode("\u200b"))
|
88 | el.insertNode(span)
|
89 | box = span.getBoundingClientRect()
|
90 |
|
91 |
|
92 | var spanParent = span.parentNode
|
93 | spanParent.removeChild(span)
|
94 | spanParent.normalize()
|
95 | }
|
96 | }
|
97 |
|
98 | var docEl = doc.documentElement
|
99 | var clientTop = docEl.clientTop || body.clientTop || 0
|
100 | var clientLeft = docEl.clientLeft || body.clientLeft || 0
|
101 | var scrollTop = window.pageYOffset || docEl.scrollTop
|
102 | var scrollLeft = window.pageXOffset || docEl.scrollLeft
|
103 |
|
104 | return {
|
105 | top: box.top + scrollTop - clientTop,
|
106 | left: box.left + scrollLeft - clientLeft
|
107 | }
|
108 | }
|
109 |
|
110 | function bodyOffset(body) {
|
111 | var top = body.offsetTop
|
112 | var left = body.offsetLeft
|
113 |
|
114 | top += parseFloat(body.style.marginTop || 0)
|
115 | left += parseFloat(body.style.marginLeft || 0)
|
116 |
|
117 | return {
|
118 | top: top,
|
119 | left: left
|
120 | }
|
121 | }
|