forked from petyosi/react-virtuoso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax.js
35 lines (28 loc) · 792 Bytes
/
max.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
// Find the maximum element height and scroll height in different
const container = document.createElement('div')
document.body.appendChild(container)
container.style.overflow = 'auto'
container.style.height = 100
const el = document.createElement('div')
container.appendChild(el)
let max = Math.pow(2, 32)
let step = max / 2
let testHeight = max
let iter = 0
while (true) {
el.style.height = `${testHeight}px`
if (container.scrollHeight !== testHeight) {
testHeight -= step
} else {
if (step > 2) {
testHeight += step
} else {
console.log(container.scrollHeight, el.offsetHeight, el.style.height)
break
}
}
step = step / 2
iter++
}
let maxOffsetHeight = testHeight
console.log('Maximum element height: ', maxOffsetHeight, { iter, step })