I have a resizeable div like this:
JavaScript
x
7
1
div{
2
resize:both;
3
overflow:hidden;
4
height:10rem;
5
width:10rem;
6
border:solid 0.5rem black;
7
}
JavaScript
1
1
1
<div id="item" ></div>
How do I invoke a function after it has completed resizing?
i.e. I don’t want the function to continuously invoke while it’s being resized,
rather I need it to invoke after the mouse has been lifted from the bottom right resizing icon
Advertisement
Answer
You could just check the mouseup
event, and track the old v new size.
The only gotcha here is if there are other ways to resize the div other than using the mouse.
JavaScript
1
18
18
1
const item = document.querySelector('#item');
2
3
function getSize() {
4
return {
5
w: item.offsetWidth,
6
h: item.offsetHeight
7
}
8
}
9
10
let old = getSize();
11
12
item.addEventListener('mouseup', () => {
13
let n = getSize();
14
if (old.w !== n.w && old.h !== n.h) {
15
console.log('resized: ' + JSON.stringify(n));
16
old = n;
17
}
18
});
JavaScript
1
11
11
1
#item {
2
resize:both;
3
overflow:hidden;
4
height:10rem;
5
width:10rem;
6
border:solid 0.5rem black;
7
}
8
9
.as-console-wrapper {
10
max-height:50px!important;
11
}
JavaScript
1
1
1
<div id="item" ></div>