mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-24 14:41:20 -08:00
12 lines
662 B
TypeScript
12 lines
662 B
TypeScript
export function inViewport(element: HTMLElement, offset: number): boolean {
|
|
const rect = element.getBoundingClientRect()
|
|
const document = element.ownerDocument
|
|
const window = document.defaultView
|
|
const height = window?.innerHeight || document.documentElement.clientHeight
|
|
const width = window?.innerWidth || document.documentElement.clientWidth
|
|
// Retain the historical edge/gap calculations while using the actual owner viewport.
|
|
const vertical = rect.top - offset <= height && rect.top + rect.height + offset >= 0
|
|
const horizontal = rect.left - offset <= width + offset && rect.left + rect.width + offset >= 0
|
|
return vertical && horizontal
|
|
}
|