{ Repost from my personal blog @ https://blog.codezero.xyz/building-interactive-elements-with-html-and-javascript-resizing }
Unlike draggable, HTML/js does not provide us with a direct spec for allowing users to graphically resize HTML DOM elements. So, we’ll be using mouse events and pointer locations to achieve the ability of resizing.
We’ll start with a box div.
<div id="box">
<div>Resize me !</div>
</div>
A little bit of CSS magic to make it look a little bit better and square.
#box {
position: relative;
width: 130px;
height: 130px;
background-color: #2196F3;
color: white;
display:flex;
justify-content:center;
align-items:center;
border-radius: 10px;
}
Now, we need a handle element. The user will be using this handle element to drag and resize the box.
<div id="box">
<div>Resize me !</div>
<div id="handle">
</div>
</div>
Now, we just have an invisible div. Let’s give it some color, make it square. We also have to position it at one corner of the box.
#handle {
background-color: #727272;
width: 10px;
height: 10px;
cursor: se-resize;
position:absolute;
right: 0;
bottom: 0;
}
The parent div#box
has the CSS property position: relative
and by setting div#handle
the property position:absolute
, we have the ability to position the handle absolutely with respect to its parent.
Also, note the cursor: se-resize
property. This instructs the browser to set the cursor to the resize cursor (↔) when the user is over it.
Now, it’s upto to javascript to take over.
var resizeHandle = document.getElementById('handle');
var box = document.getElementById('box');
For resizing, the user would click on the handle and drag it. So, we need to start resizing the moment the user presses and holds on the handle. Let’s setup a function to listen for the mousedown
event.
resizeHandle.addEventListener('mousedown', initialiseResize, false);
the initialiseResize
function should do two things:
- Resize the box every time the mouse pointer moves.
- Listen for
mouseup
event so that the event listeners can be removed as soon as the user is done resizing.
function initialiseResize(e) {
window.addEventListener('mousemove', startResizing, false);
window.addEventListener('mouseup', stopResizing, false);
}
function startResizing(e) {
// Do resize here
}
function stopResizing(e) {
window.removeEventListener('mousemove', startResizing, false);
window.removeEventListener('mouseup', stopResizing, false);
}
To resize the box according to the user’s mouse pointer movements, we’ll be taking the current x and y coordinates of the mouse pointer (in pixels) and change the box’s height and width accordingly.
function startResizing(e) {
box.style.width = (e.clientX) + 'px';
box.style.height = (e.clientY) + 'px';
}
e.clientX
gives the mouse pointer’s X coordinate and e.clientY
gives the mouse pointer’s Y coordinate
Now, this works. But this would only work as expected if the box is placed in the top-left corner of the page. We’ll have to compensate for the box’s left and top offsets. (position from the left and top edges of the page)
function startResizing(e) {
box.style.width = (e.clientX - box.offsetLeft) + 'px';
box.style.height = (e.clientY - box.offsetTop) + 'px';
}
There you go We can now resize the box !
https://jsfiddle.net/niranjan94/w8k1ffju/embedded