-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
dom.ts
32 lines (28 loc) · 842 Bytes
/
dom.ts
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
/*
* @Author: Innei
* @Date: 2020-05-24 17:03:12
* @LastEditTime: 2021-01-14 13:16:05
* @LastEditors: Innei
* @FilePath: /web/utils/dom.ts
* @Copyright
*/
import type { BaseSyntheticEvent } from 'react'
export const stopEventDefault = <T extends BaseSyntheticEvent>(e: T) => {
e.preventDefault()
e.stopPropagation()
}
export function getElementViewTop<T extends HTMLElement>(element: T) {
let actualTop = element.offsetTop
let current = element.offsetParent as HTMLElement
while (current !== null) {
actualTop += current.offsetTop
current = current.offsetParent as HTMLElement
}
let elementScrollTop = 0
if (document.compatMode == 'BackCompat') {
elementScrollTop = document.body.scrollTop
} else {
elementScrollTop = document.documentElement.scrollTop
}
return actualTop - elementScrollTop
}