-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathbind.ts
More file actions
32 lines (26 loc) · 997 Bytes
/
bind.ts
File metadata and controls
32 lines (26 loc) · 997 Bytes
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
import { getPrefix } from './prefix';
export interface BindHook {
before?: () => void;
after?: () => void;
}
export default function (
observer: IntersectionObserver | null,
hook?: BindHook
) {
// Before Hook
hook && typeof hook.before === 'function' && hook.before();
// Fetch all DOM elements with [tg-name] attribute and set the current top & left offset
const prefix = getPrefix()
document
.querySelectorAll<HTMLElement>(`[${prefix}name]`)
.forEach((element) => {
const { top, height, left, width } = element.getBoundingClientRect();
element.style.setProperty(`--${prefix}top`, `${top + window.scrollY}`);
element.style.setProperty(`--${prefix}left`, `${left + window.scrollX}`);
element.style.setProperty(`--${prefix}height`, `${height}`);
element.style.setProperty(`--${prefix}width`, `${width}`);
observer && observer.observe(element);
});
// After Hook
hook && typeof hook.after === 'function' && hook.after();
}