Skip to content
This repository was archived by the owner on Oct 22, 2021. It is now read-only.

Commit 2805f1d

Browse files
committed
✨ Draggable modal windows
1 parent 47cb055 commit 2805f1d

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

src/assets/css/modal.css

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
div.modal_popup {
2-
position: fixed;
2+
position: absolute;
33
min-width: 30vw;
44
background: var(--color_light_black);
55
border-color: rgb(var(--color_r), var(--color_g), var(--color_b));;
@@ -31,6 +31,8 @@ div.modal_popup.blink {
3131
}
3232

3333
div.modal_popup > h1 {
34+
width: 100%;
35+
cursor: move;
3436
font-size: 4vh;
3537
margin: 0vh;
3638
margin-bottom: 2vh;

src/classes/modal.class.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,69 @@ class Modal {
8383
}
8484
window.modals[this.id] = this;
8585
document.body.appendChild(element);
86+
87+
// Allow dragging the modal around
88+
let draggedModal = document.getElementById(`modal_${this.id}`);
89+
let dragTarget = document.querySelector(`div#modal_${this.id} > h1:first-child`);
90+
91+
draggedModal.zindex = draggedModal.getAttribute("style");
92+
93+
let rect = draggedModal.getBoundingClientRect();
94+
draggedModal.posX = rect.left;
95+
draggedModal.posY = rect.top;
96+
97+
// Mouse
98+
function modalMousedownHandler(e) {
99+
draggedModal.lastMouseX = e.clientX;
100+
draggedModal.lastMouseY = e.clientY;
101+
102+
draggedModal.setAttribute("style", `${draggedModal.zindex}background: rgba(var(--color_r), var(--color_g), var(--color_b), 0.5);left: ${draggedModal.posX}px;top: ${draggedModal.posY}px;`);
103+
104+
window.addEventListener("mousemove", modalMousemoveHandler);
105+
window.addEventListener("mouseup", modalMouseupHandler);
106+
}
107+
function modalMousemoveHandler(e) {
108+
draggedModal.posX = draggedModal.posX + (e.clientX - draggedModal.lastMouseX);
109+
draggedModal.posY = draggedModal.posY + (e.clientY - draggedModal.lastMouseY);
110+
draggedModal.lastMouseX = e.clientX;
111+
draggedModal.lastMouseY = e.clientY;
112+
113+
draggedModal.setAttribute("style", `${draggedModal.zindex}background: rgba(var(--color_r), var(--color_g), var(--color_b), 0.5);left: ${draggedModal.posX}px;top: ${draggedModal.posY}px;`);
114+
}
115+
function modalMouseupHandler(e) {
116+
window.removeEventListener("mousemove", modalMousemoveHandler);
117+
draggedModal.setAttribute("style", `${draggedModal.zindex}left: ${draggedModal.posX}px;top: ${draggedModal.posY}px;`);
118+
119+
window.removeEventListener("mouseup", modalMouseupHandler);
120+
}
121+
dragTarget.addEventListener("mousedown", modalMousedownHandler);
122+
123+
// Touch
124+
function modalTouchstartHandler(e) {
125+
draggedModal.lastMouseX = e.changedTouches[0].clientX;
126+
draggedModal.lastMouseY = e.changedTouches[0].clientY;
127+
128+
draggedModal.setAttribute("style", `${draggedModal.zindex}background: rgba(var(--color_r), var(--color_g), var(--color_b), 0.5);left: ${draggedModal.posX}px;top: ${draggedModal.posY}px;`);
129+
130+
window.addEventListener("touchmove", modalTouchmoveHandler);
131+
window.addEventListener("touchend", modalTouchendHandler);
132+
}
133+
function modalTouchmoveHandler(e) {
134+
draggedModal.posX = draggedModal.posX + (e.changedTouches[0].clientX - draggedModal.lastMouseX);
135+
draggedModal.posY = draggedModal.posY + (e.changedTouches[0].clientY - draggedModal.lastMouseY);
136+
draggedModal.lastMouseX = e.clientX;
137+
draggedModal.lastMouseY = e.clientY;
138+
139+
draggedModal.setAttribute("style", `${draggedModal.zindex}background: rgba(var(--color_r), var(--color_g), var(--color_b), 0.5);left: ${draggedModal.posX}px;top: ${draggedModal.posY}px;`);
140+
}
141+
function modalTouchendHandler(e) {
142+
window.removeEventListener("touchmove", modalTouchmoveHandler);
143+
draggedModal.setAttribute("style", `${draggedModal.zindex}left: ${draggedModal.posX}px;top: ${draggedModal.posY}px;`);
144+
145+
window.removeEventListener("touchend", modalTouchendHandler);
146+
}
147+
dragTarget.addEventListener("touchstart", modalTouchstartHandler);
148+
86149
return this.id;
87150
}
88151
}

0 commit comments

Comments
 (0)