forked from steemit/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscroll-to.js
More file actions
67 lines (56 loc) · 1.95 KB
/
scroll-to.js
File metadata and controls
67 lines (56 loc) · 1.95 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
/**
* Copied from `element-scroll-to`, but edited to account for `window` being
* inside of iframes.
*
* https://github.com/webmodules/element-scroll-to
*/
import getWindow from 'get-window'
function scrollWrapperElements(element, options) {
let window = getWindow(element)
let elementRect = element.getBoundingClientRect()
let wrapper = element.parentNode
while (wrapper != window.document.body) {
let wrapperRect = wrapper.getBoundingClientRect()
let margin = options.margin
let deltaX = 0
let deltaY = 0
if (elementRect.top < wrapperRect.top + margin) {
deltaY = elementRect.top - wrapperRect.top - margin
} else if (elementRect.bottom > wrapperRect.bottom - margin) {
deltaY = elementRect.bottom - wrapperRect.bottom + margin
}
if (elementRect.left < wrapperRect.left + margin) {
deltaX = elementRect.left - wrapperRect.left - margin
} else if (elementRect.right > wrapperRect.right - margin) {
deltaX = elementRect.right - wrapperRect.right + margin
}
wrapper.scrollTop += deltaY
wrapper.scrollLeft += deltaX
wrapper = wrapper.parentNode
}
return elementRect
}
function scrollWindow(element, options, elementRect) {
let window = getWindow(element)
let margin = options.margin
let deltaX = 0
let deltaY = 0
if (elementRect.top < 0 + margin) {
deltaY = elementRect.top - margin
} else if (elementRect.bottom > window.innerHeight - margin) {
deltaY = elementRect.bottom - window.innerHeight + margin
}
if (elementRect.left < 0 + margin) {
deltaX = elementRect.left - margin
} else if (elementRect.right > window.innerWidth - margin) {
deltaX = elementRect.right - window.innerWidth + margin
}
window.scrollBy(deltaX, deltaY)
}
function scrollTo(element, options) {
options = options || {}
options.margin = options.margin || 0
let rect = scrollWrapperElements(element, options)
scrollWindow(element, options, rect)
}
export default scrollTo