-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.next.js
126 lines (113 loc) · 2.78 KB
/
index.next.js
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const doc = document
const win = window
const body = doc.body
const html = doc.documentElement
const ZERO = 0
/**
* Simple function convert in boolean any value and return true if its value was truthy
* @private
* @param {*} v - anything
* @returns { boolean } true if truthy
*/
const isTruthy = v => !!v
/**
* Get the max value from a list of arguments filtering the falsy values
* @private
* @param {...number} args - list of numbers
* @returns { number } the highest value
*/
const max = (...args) => Math.max(ZERO, ...args.filter(isTruthy), ZERO) // eslint-disable-line
/**
* Return the size of the scrollbar that depends on the browser or device used on the client
* @returns { number } - the browser scrollbar width
*/
export function scrollbarWidth() {
// Create the measurement node
const div = doc.createElement('div')
Object.assign(div.style, {
width: '100px',
height: '100px',
overflow: 'scroll',
position: 'fixed',
opacity: '0'
})
doc.body.appendChild(div)
// Read values
const { offsetWidth, clientWidth } = div
// Delete helper element
doc.body.removeChild(div)
return max(offsetWidth - clientWidth)
}
/**
* Get the height of the whole page
* @returns { number } height in px of the document
*/
export function documentHeight() {
return max(
body.scrollHeight,
body.offsetHeight,
html.clientHeight,
html.scrollHeight,
html.offsetHeight
)
}
/**
* Get the width of the whole page
* @returns { number } width in px of the document
*/
export function documentWidth() {
return max(
body.scrollWidth,
body.offsetWidth,
html.clientWidth,
html.scrollWidth,
html.offsetWidth
)
}
/**
* Return amount of px scrolled from the top of the document
* @returns { number } scroll top value in px
*/
export function scrollTop() {
return max(
win.scrollY,
win.pageYOffset,
doc.documentElement.scrollTop
)
}
/**
* Return amount of px scrolled from the left of the document
* @returns { number } scroll left value in px
*/
export function scrollLeft() {
return max(
win.scrollX,
win.pageXOffset,
doc.documentElement.scrollLeft
)
}
/**
* Get the offset top of any DOM element
* @param { HTMLElement } el - the element we need to check
* @returns { number } the element y position in px
*/
export function elementOffsetTop(el) {
return max(scrollTop() + el.getBoundingClientRect().top)
}
/**
* Get the offset left of any DOM element
* @param { HTMLElement } el - the element we need to check
* @returns { number } the element x position in px
*/
export function elementOffsetLeft(el) {
return max(scrollLeft() + el.getBoundingClientRect().left)
}
export default {
scrollbarWidth,
documentHeight,
documentWidth,
scrollTop,
scrollLeft,
elementOffsetTop,
elementOffsetLeft
}