-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
49 lines (39 loc) · 1.49 KB
/
Copy pathscript.js
File metadata and controls
49 lines (39 loc) · 1.49 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const container = document.getElementById('todo-container');
let isDragging = false;
let startMouseX = 0;
let startMouseY = 0;
let startLeft = 0;
let startTop = 0;
// Position container absolutely initially
container.style.position = 'absolute';
container.style.top = container.offsetTop + 'px';
container.style.left = container.offsetLeft + 'px';
container.addEventListener('mousedown', (e) => {
// Only drag if clicked on container background (not input, button, list)
if (e.target.closest('input, button, label, li, ul')) return;
isDragging = true;
startMouseX = e.clientX;
startMouseY = e.clientY;
startLeft = container.offsetLeft;
startTop = container.offsetTop;
document.body.style.userSelect = 'none';
});
document.addEventListener('mousemove', (e) => {
if (!isDragging) return;
let newLeft = startLeft + (e.clientX - startMouseX);
let newTop = startTop + (e.clientY - startMouseY);
// Constrain within viewport horizontally
const maxLeft = window.innerWidth - container.offsetWidth;
newLeft = Math.max(0, Math.min(newLeft, maxLeft));
// Constrain vertically within page
const maxTop = Math.max(document.body.scrollHeight - container.offsetHeight, 0);
newTop = Math.max(0, Math.min(newTop, maxTop));
container.style.left = newLeft + 'px';
container.style.top = newTop + 'px';
});
document.addEventListener('mouseup', () => {
if (isDragging) {
isDragging = false;
document.body.style.userSelect = 'auto';
}
});