0

I am trying to create a dragabble resizable dialog box, but I can't seam to get it to resize from right to left, bottom to top. I want to be able to resize the div without changing its position. If you take a look at the demo on codeine, you can drag from bottom right corner. It's as if its width resize is using the center of the div as its transform origin. of course setting the transform origin property in css to top left does nothing on resizing.

Please any help is much appreciated.

link to codepen

    $( "#os_window" ).draggable({ handle: "#window_top_bar" });
    $( "#os_window" ).resizable();

im finding very little in terms of direction on this

2
  • So the problem arises when placing that div using transform:translate. Commented Sep 12, 2024 at 4:03
  • Please check the answer Commented Sep 18, 2024 at 21:55

1 Answer 1

0

I have tried it too but not working as per expected. Also tried to set the positions manually bud didn't worked as well.

So finally I have created another example for this situation.

let dragEl = null;
let offsetX = 0;
let offsetY = 0;

document.querySelector('.box-header').addEventListener('mousedown', (event) => {
  dragEl = event.target.closest('.box');
  offsetX = event.clientX - dragEl.offsetLeft;
  offsetY = event.clientY - dragEl.offsetTop;

  document.addEventListener('mousemove', onMouseMove);
  document.addEventListener('mouseup', onMouseUp);
});

function onMouseMove(event) {
  if (!dragEl) return;
  dragEl.style.left = `${event.clientX - offsetX}px`;
  dragEl.style.top = `${event.clientY - offsetY}px`;
}

function onMouseUp() {
  dragEl = null;
  document.removeEventListener('mousemove', onMouseMove);
  document.removeEventListener('mouseup', onMouseUp);
}
.box {
  width: 300px;
  height: 200px;
  background-color: #fff;
  border: 1px solid #ccc;
  position: absolute;
  resize: both;
  overflow: hidden;
}

.box-header {
  background-color: #ccc;
  padding: 10px 15px;
  cursor: move;
}
<div class="box" data-draggable="true" data-resizable="true">
  <div class="box-header drag-handle" data-drag-handle="true">Drag here</div>
  <div class="box-body">Drag and resize box</div>
</div> 

<Codepen Url/>

Not the answer you're looking for? Browse other questions tagged or ask your own question.