2019-04-24 19:53:53 +08:00
|
|
|
export type BindElement = HTMLElement | Window | null | undefined;
|
|
|
|
|
2021-10-25 19:43:17 +08:00
|
|
|
export function getTargetRect(target: BindElement): DOMRect {
|
2019-04-24 19:53:53 +08:00
|
|
|
return target !== window
|
|
|
|
? (target as HTMLElement).getBoundingClientRect()
|
2021-10-25 19:43:17 +08:00
|
|
|
: ({ top: 0, bottom: window.innerHeight } as DOMRect);
|
2019-04-24 19:53:53 +08:00
|
|
|
}
|
|
|
|
|
2023-02-24 09:04:03 +08:00
|
|
|
export function getFixedTop(placeholderRect: DOMRect, targetRect: DOMRect, offsetTop?: number) {
|
|
|
|
if (offsetTop !== undefined && targetRect.top > placeholderRect.top - offsetTop) {
|
2019-04-24 19:53:53 +08:00
|
|
|
return offsetTop + targetRect.top;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
export function getFixedBottom(
|
2023-02-24 09:04:03 +08:00
|
|
|
placeholderRect: DOMRect,
|
2021-10-25 19:43:17 +08:00
|
|
|
targetRect: DOMRect,
|
|
|
|
offsetBottom?: number,
|
2019-05-07 14:57:32 +08:00
|
|
|
) {
|
2023-02-24 09:04:03 +08:00
|
|
|
if (offsetBottom !== undefined && targetRect.bottom < placeholderRect.bottom + offsetBottom) {
|
2019-04-24 19:53:53 +08:00
|
|
|
const targetBottomOffset = window.innerHeight - targetRect.bottom;
|
|
|
|
return offsetBottom + targetBottomOffset;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|