-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathindex.js
More file actions
74 lines (61 loc) · 2.08 KB
/
index.js
File metadata and controls
74 lines (61 loc) · 2.08 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var support = require('dom-support')
var getDocument = require('get-document')
var withinElement = require('within-element')
/**
* Get offset of a DOM Element or Range within the document.
*
* @param {DOMElement|Range} el - the DOM element or Range instance to measure
* @return {Object} An object with `top` and `left` Number values
* @public
*/
module.exports = function offset(el) {
var doc = getDocument(el)
if (!doc) return
// Make sure it's not a disconnected DOM node
if (!withinElement(el, doc)) return
var body = doc.body
if (body === el) {
return bodyOffset(el)
}
var box = { top: 0, left: 0 }
if ( typeof el.getBoundingClientRect !== "undefined" ) {
// If we don't have gBCR, just use 0,0 rather than error
// BlackBerry 5, iOS 3 (original iPhone)
box = el.getBoundingClientRect()
if (el.collapsed && box.left === 0 && box.top === 0) {
// collapsed Range instances sometimes report 0, 0
// see: http://stackoverflow.com/a/6847328/376773
var span = doc.createElement("span");
// Ensure span has dimensions and position by
// adding a zero-width space character
span.appendChild(doc.createTextNode("\u200b"));
el.insertNode(span);
box = span.getBoundingClientRect();
// Remove temp SPAN and glue any broken text nodes back together
var spanParent = span.parentNode;
spanParent.removeChild(span);
spanParent.normalize();
}
}
var docEl = doc.documentElement
var clientTop = docEl.clientTop || body.clientTop || 0
var clientLeft = docEl.clientLeft || body.clientLeft || 0
var scrollTop = window.pageYOffset || docEl.scrollTop
var scrollLeft = window.pageXOffset || docEl.scrollLeft
return {
top: box.top + scrollTop - clientTop,
left: box.left + scrollLeft - clientLeft
}
}
function bodyOffset(body) {
var top = body.offsetTop
var left = body.offsetLeft
if (support.doesNotIncludeMarginInBodyOffset) {
top += parseFloat(body.style.marginTop || 0)
left += parseFloat(body.style.marginLeft || 0)
}
return {
top: top,
left: left
}
}