-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetBoundingClientRect.js
More file actions
59 lines (52 loc) · 1.52 KB
/
Copy pathgetBoundingClientRect.js
File metadata and controls
59 lines (52 loc) · 1.52 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
import isElement from './isElement'
import isLayoutViewport from './isLayoutViewport'
import getVisualViewport from './getVisualViewport'
/**
* 获取 DOM 元素的 getBoundingClientRect 数据
* ========================================================================
* @method getBoundingClientRect
* @since 1.7.0
* @see https://github.com/floating-ui/floating-ui
* @param {HTMLElement} el
* @param {Boolean} [includeScale]
* @param {Boolean} [isFixedStrategy]
* @return {Object}
*/
const getBoundingClientRect = (
el,
includeScale = false,
isFixedStrategy = false
) => {
const round = Math.round
const clientRect = el.getBoundingClientRect()
const visualViewport = getVisualViewport(el)
const addVisualOffsets = !isLayoutViewport() && isFixedStrategy
let scaleX = 1
let scaleY = 1
if (includeScale && isElement(el)) {
let offsetWidth = el.offsetWidth
let offsetHeight = el.offsetHeight
scaleX = offsetWidth > 0 ? round(clientRect.width) / offsetWidth || 1 : 1
scaleY = offsetHeight > 0 ? round(clientRect.height) / offsetHeight || 1 : 1
}
const x =
(clientRect.left +
(addVisualOffsets ? visualViewport?.offsetLeft ?? 0 : 0)) /
scaleX
const y =
(clientRect.top + (addVisualOffsets ? visualViewport?.offsetTop ?? 0 : 0)) /
scaleY
const width = clientRect.width / scaleX
const height = clientRect.height / scaleY
return {
width,
height,
top: y,
right: x + width,
bottom: y + height,
left: x,
x,
y
}
}
export default getBoundingClientRect