-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdomUtils.ts
More file actions
24 lines (24 loc) · 1008 Bytes
/
domUtils.ts
File metadata and controls
24 lines (24 loc) · 1008 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
// DOM utility functions
export function isElementInViewport(el: Element): boolean {
const rect = el.getBoundingClientRect();
return rect.top >= 0 && rect.left >= 0 && rect.bottom <= window.innerHeight && rect.right <= window.innerWidth;
}
export function copyToClipboard(text: string): Promise<void> {
if (navigator.clipboard) return navigator.clipboard.writeText(text);
const textarea = document.createElement('textarea');
textarea.value = text;
textarea.style.position = 'fixed';
textarea.style.opacity = '0';
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
return Promise.resolve();
}
export function getScrollPercentage(): number {
const h = document.documentElement;
const b = document.body;
const scrollTop = h.scrollTop || b.scrollTop;
const scrollHeight = (h.scrollHeight || b.scrollHeight) - h.clientHeight;
return scrollHeight > 0 ? Math.round((scrollTop / scrollHeight) * 100) : 0;
}