Draggable FULL <div>
How to make 4 corners resize-able? How to make just header/borders draggable? so image can be zoomed/scrolled z-index content/menus?
by trentHarlem
HTML
<h2>Draggable Resize-able</h2>
<div id="mydiv">
<div class='resizer ne'></div>
<div class='resizer nw'></div>
<div class='resizer sw'></div>
<div class='resizer se'></div>
<div id="mydivheader">Click anywhere to move
<div id='content'>
<!-- <div id='image-container'><img height='auto' width='100%' src='https://picsum.photos/600' /></div> -->
</div>
</div>
</div>
CSS
* {
margin: 0;
}
#mydiv {
position: absolute; /* NECESSARY */
background-color: whitesmoke;
text-align: center;
border: 1px solid #222;
height: 200px;
width: 200px;
/* resize: both; /* CSS RESIZE */
overflow: auto; /* CSS RESIZE */
}
#mydivheader {
padding: 10px;
cursor: move;
background-color: dodgerblue;
color: #fff;
}
#content {
color: #000;
margin: 0px;
background-color: whitesmoke;
}
/* ::-webkit-resizer {
position: absolute;
height: 20px;
width: 20px;
border-top-left-radius: 25px;
background-color: #dd0;
z-index: 2;
} */
.resizer {
position: absolute;
height: 20px;
width: 20px;
/* border-top-left-radius: 25px; */
background-color: #dd0;
z-index: 12;
}
.resizer.nw {
top: -1px;
left: -1px;
cursor: nw-resize;
border-bottom-right-radius: 25px;
}
.resizer.ne {
top: -1px;
right: -1px;
cursor: ne-resize;
border-bottom-left-radius: 25px;
}
.resizer.sw {
bottom: -1px;
left: -1px;
cursor: sw-resize;
border-top-right-radius: 25px;
}
.resizer.se {
bottom: -1px;
right: -1px;
cursor: se-resize;
border-top-left-radius: 25px;
}
JavaScript
const myDiv = document.getElementById('mydiv')
//Make the DIV element draggagle:
dragElement(myDiv);
let isResizing = false;
function dragElement(elmnt) {
let pos1 = 0,
pos2 = 0,
pos3 = 0,
pos4 = 0;
if (document.getElementById(elmnt.id + "header")) {
//if present, the header is where you move the DIV from:
document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
} else {
//otherwise, move the DIV from anywhere inside the DIV:
elmnt.onmousedown = dragMouseDown;
}
function dragMouseDown(e) {
if (!isResizing) {
e = e || window.event;
e.preventDefault();
// get the mouse cursor position at startup:
pos3 = e.clientX;
pos4 = e.clientY;
document.onmouseup = closeDragElement;
// call a function whenever the cursor moves:
document.onmousemove = elementDrag;
}
}
function elementDrag(e) {
if (!isResizing) {
e = e || window.event;
e.preventDefault();
// calculate the new cursor position:
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
// set the element's new position:
elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
}
}
function closeDragElement() {
//stop moving when mouse button is released:
document.onmouseup = null;
document.onmousemove = null;
}
}
const resizers = document.querySelectorAll('.resizer')
let currentResizer
for (let resizer of resizers) {
resizer.addEventListener('mousedown', mouseDown)
function mouseDown(e) {
currentResizer = e.target
isResizing = true;
let posX = e.clientX;
let posY = e.clientY;
myDiv.addEventListener('mousemove', mouseMove)
myDiv.addEventListener('mouseup', mouseUp)
function mouseMove(e) {
const rect = myDiv.getBoundingClientRect()
if (currentResizer.classList.contains('se')) {
...