JSFiddle - React, Tailwind, and code Playground
by arawn45
HTML
<div class='resizable'>
<div class='resizers'>
<div class='resizer top-right'></div>
</div>
</div>
<br>
<div class='resizable'>
<div class='resizers'>
<div class='resizer top-right'></div>
</div>
</div>
<br>
<div class='resizable'>
<div class='resizers'>
<div class='resizer top-right'></div>
</div>
</div>
CSS
body,
html {
background: black;
}
.resizable {
background: white;
width: 100px;
height: 100px;
position: relative;
top: 100px;
left: 100px;
}
.resizable .resizers{
width: 100%;
height: 100%;
border: 3px solid #4286f4;
box-sizing: border-box;
}
.resizable .resizers .resizer{
width: 5px;
height: 100%;
position: absolute;
}
.resizable .resizers .resizer.top-right {
right: -1px;
top: -1px;
cursor: ew-resize;
}
JavaScript
/*Make resizable div by Hung Nguyen*/
function makeResizableDiv(div) {
const element = document.querySelector(div);
const resizers = document.querySelectorAll(div + ' .resizer')
const minimum_size = 20;
let original_width = 0;
let original_height = 0;
let original_x = 0;
let original_y = 0;
let original_mouse_x = 0;
let original_mouse_y = 0;
for (let i = 0;i < resizers.length; i++) {
const currentResizer = resizers[i];
currentResizer.addEventListener('mousedown', function(e) {
e.preventDefault()
original_width = parseFloat(getComputedStyle(element, null).getPropertyValue('width').replace('px', ''));
//original_height = parseFloat(getComputedStyle(element, null).getPropertyValue('height').replace('px', ''));
original_x = element.getBoundingClientRect().left;
original_y = element.getBoundingClientRect().top;
original_mouse_x = e.pageX;
original_mouse_y = e.pageY;
window.addEventListener('mousemove', resize)
window.addEventListener('mouseup', stopResize)
})
function resize(e) {
if (currentResizer.classList.contains('bottom-right')) {
const width = original_width + (e.pageX - original_mouse_x);
const height = original_height + Math.round((e.pageY - original_mouse_y)/20)*20
if (width > minimum_size) {
element.style.width = width + 'px'
}
if (height > minimum_size) {
//element.style.height = height + 'px'
}
}
else if (currentResizer.classList.contains('bottom-left')) {
const height = original_height + (e.pageY - original_mouse_y)
const width = original_width - (e.pageX - original_mouse_x)
if (height > minimum_size) {
//element.style.height = height + 'px'
}
if (width > minimum_size) {
element.style.width = width + 'px'
element.style.left = original_x + (e.pageX - original_mouse_x) + 'px'
}
}
else if...