-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting resizable frames.js
More file actions
50 lines (44 loc) · 1.78 KB
/
Copy pathtesting resizable frames.js
File metadata and controls
50 lines (44 loc) · 1.78 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
50
function onload() {
dragElement(document.getElementById("separator"), "H");
}
// This function is used for dragging and moving
function dragElement(element, direction, handler) {
// Two variables as the cursor's co-ordinates
const drag = { x: 0, y: 0 };
const delta = { x: 0, y: 0 };
/* If present, the handler is where you move the DIV from
otherwise, move the DIV from anywhere inside the DIV */
handler ? (handler.onmousedown = dragMouseDown) : (element.onmousedown = dragMouseDown);
// A function that will be called whenever the down event of the mouse is raised
function dragMouseDown(e) {
drag.x = e.clientX;
drag.y = e.clientY;
document.onmousemove = onMouseMove;
document.onmouseup = () => {
document.onmousemove = document.onmouseup = null;
};
}
// A function that will be called whenever the up event of the mouse is raised
function onMouseMove(e) {
const currentX = e.clientX;
const currentY = e.clientY;
delta.x = currentX - drag.x;
delta.y = currentY - drag.y;
const offsetLeft = element.offsetLeft;
const offsetTop = element.offsetTop;
const first = document.getElementById("first");
const second = document.getElementById("second");
let firstWidth = first.offsetWidth;
let secondWidth = second.offsetWidth;
if (direction === "H") {
// Horizontal
element.style.left = offsetLeft + delta.x + "px";
firstWidth += delta.x;
secondWidth -= delta.x;
}
drag.x = currentX;
drag.y = currentY;
first.style.width = firstWidth + "px";
second.style.width = secondWidth + "px";
}
}