So this is a bit of a strange question. Programming a web application with a resizable div. I'm using the CSS resize property at the moment as it seemed like it was the easiest way to do it. The problem is my box has a locked bottom and left position (which I want to maintain), but the resize corner chosen by default is the lower right one, which produces weird results for resizing the top. Looked around online but couldn't find a way to move that corner to the upper right instead of the bottom right. Any advice on how to produce this result? Also not opposed to using a different method of resizing. Here's the current style being applied to the box:
display: block;
font-size: 9pt;
position: absolute;
bottom: 50px;
left: 20px;
padding: 10px;
min-height: 0;
min-width: 0;
border: solid 1px black;
width:400px;
height:400px;
resize: both;
overflow-x: auto;
overflow-y: hidden;
And here's a picture of the div in question with the corner that currently has the resize tool on it boxed. Any advice would be greatly appreciated. I'm more than happy to elaborate on things I might've missed as well. Thanks!
Here's one starting point. I essentially created a custom resize icon in the upper right corner. I then used 'mousedown', 'mousemove' and 'mouseup' events to track resizing activity. You might be able to do something similar using 'drag' and related events.
Note the 'mousedown' is on the resize icon, while 'mousemove' and 'mouseup' are on the document body (or some other larger element behind the resizable div).
I've hard-coded the width and height in the JavaScript for the sake of simplicity. That means those values exist in two different places which is not good. You should probably put them only in JavaScript or only in CSS, not in both as I have done.
I needed to put the resizable div into a container that filled the body. Otherwise 'mousemove' events outside of the resizable div were not registered. This might not be an issue if you already have other content "behind" your absolutely positioned resizable element.
For your information: I also initially tried using the native resize functionality. I used transform: rotate(-90deg)
to move the resize corner to the upper right, then put in an inner div with transform: rotate(90deg)
to make the inside content the right-way-up again. However, while this put the resize corner in the correct place, the resizing itself was completely messed up, as the mouse movements and the actually resizing itself were off by 90 degrees. It's a little hard to describe in words, but suffice it to say that, without some major re-working (or perhaps some brilliant code or hidden function) I couldn't get that strategy to work.
var
doc = document,
ht = 400,
wd = 400,
main = document.querySelector("#resizable"),
x, y, dx, dy;
var startResize = function(evt) {
x = evt.screenX;
y = evt.screenY;
};
var resize = function(evt) {
dx = evt.screenX - x;
dy = evt.screenY - y;
x = evt.screenX;
y = evt.screenY;
wd += dx;
ht -= dy;
main.style.width = wd + "px";
main.style.height = ht + "px";
};
rsz.addEventListener("mousedown", function(evt) {
startResize(evt);
doc.body.addEventListener("mousemove", resize);
doc.body.addEventListener("mouseup", function() {
doc.body.removeEventListener("mousemove", resize);
});
});
#container {
position: absolute;
border: solid red 1px;
margin: 0;
height: 100%;
width: 100%;
}
#resizable {
display: block;
font-size: 9pt;
position: absolute;
bottom: 50px;
left: 20px;
padding: 10px;
min-height: 0;
min-width: 0;
border: solid 1px black;
width: 400px;
height: 400px;
overflow-x: auto;
overflow-y: hidden;
}
#rsz {
position: absolute;
right: 0;
top: 0;
width: 20px;
height: 20px;
background-color: rgba(255, 0, 0, 0.5);
}
#original {}
<div id="container">
<div id="resizable">
<div id="rsz"></div>
(some content)
</div>
</div>
If you run this code snippet from within Stack Overflow, you need to expand the running view area by clicking on "Full page" after clicking "Run code snippet".