ant-design/components/affix/utils.ts

33 lines
945 B
TypeScript
Raw Permalink Normal View History

export type BindElement = HTMLElement | Window | null | undefined;
export function getTargetRect(target: BindElement): DOMRect {
return target !== window
? (target as HTMLElement).getBoundingClientRect()
: ({ top: 0, bottom: window.innerHeight } as DOMRect);
}
2023-02-24 09:04:03 +08:00
export function getFixedTop(placeholderRect: DOMRect, targetRect: DOMRect, offsetTop?: number) {
2024-01-28 13:31:58 +08:00
if (
offsetTop !== undefined &&
Math.round(targetRect.top) > Math.round(placeholderRect.top) - offsetTop
) {
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,
targetRect: DOMRect,
offsetBottom?: number,
2019-05-07 14:57:32 +08:00
) {
2024-01-28 13:31:58 +08:00
if (
offsetBottom !== undefined &&
Math.round(targetRect.bottom) < Math.round(placeholderRect.bottom) + offsetBottom
) {
const targetBottomOffset = window.innerHeight - targetRect.bottom;
return offsetBottom + targetBottomOffset;
}
return undefined;
}