Я делаю dragable и изменяемый размер div. Dragible работает нормально. Я хочу, чтобы div мог изменять ширину и высоту движением мыши после нажатия на «resizer», который расположен во всех углах. Я понял, как сделать правый нижний «resizer», и он отлично работает, но я не могу сделать левый нижний. У кого-нибудь есть идеи как?
window.addEventListener('mouseup', mouseUpDragElement, false);
document.getElementById("dragdiv").addEventListener('mousedown', mouseDownDragElement, false);
document.getElementById("dragdiv").addEventListener('mousedown', mouseDownResizeElement, false);
var elementdragTop;
var elementdragLeft;
function mouseDownDragElement(e) {
window.addEventListener("mousemove", myFunctionDragElement, true);
elementdragTop = e.clientX - document.getElementById("dragdiv").offsetLeft;
elementdragLeft = e.clientY - document.getElementById("dragdiv").offsetTop;
}
function mouseUpDragElement() {
window.removeEventListener("mousemove", myFunctionDragElement, true);
}
function myFunctionDragElement(e) {
var x = e.clientX;
var y = e.clientY;
document.getElementById("dragdiv").style.position = "absolute";
document.getElementById("dragdiv").style.left = (x - elementdragTop) + 'px';
document.getElementById("dragdiv").style.top = (y - elementdragLeft) + 'px';
}
function mouseDownResizeElement(e) {
var resizertopleft = document.createElement("DIV");
resizertopleft.id = "resizer-top-left";
document.getElementById("dragdiv").appendChild(resizertopleft);
var resizertopright = document.createElement("DIV");
resizertopright.id = "resizer-top-right";
document.getElementById("dragdiv").appendChild(resizertopright);
var resizerbottomleft = document.createElement("DIV");
resizerbottomleft.id = "resizer-bottom-left";
resizerbottomleft.addEventListener('mousedown', mouseDownResizeElementResizerBottomLeft, false);
document.getElementById("dragdiv").appendChild(resizerbottomleft);
var resizerbottomright = document.createElement("DIV");
resizerbottomright.id = "resizer-bottom-right";
resizerbottomright.addEventListener('mousedown', mouseDownResizeElementResizerBottomRight, false);
document.getElementById("dragdiv").appendChild(resizerbottomright);
}
function mouseUpResizeElement(e) {
document.getElementById("resizer-top-left").remove();
}
// BUTTON BOTTOM RIGHT
function mouseDownResizeElementResizerBottomRight() {
window.addEventListener("mousemove", mouseMoveResizeElementResizerBottomRight, true);
document.getElementById("dragdiv").addEventListener('mouseup', mouseUpResizeElementResizerBottomRight, true);
}
function mouseMoveResizeElementResizerBottomRight(e) {
document.getElementById("dragdiv").addEventListener('mouseup', mouseUpResizeElementResizerBottomRight, true);
window.removeEventListener("mousemove", myFunctionDragElement, true);
document.getElementById("dragdiv").style.width = e.pageX - document.getElementById("dragdiv").getBoundingClientRect().left + 'px';
document.getElementById("dragdiv").style.height = e.pageY - document.getElementById("dragdiv").getBoundingClientRect().top + 'px';
}
function mouseUpResizeElementResizerBottomRight() {
window.removeEventListener("mousemove", mouseMoveResizeElementResizerBottomRight, true);
}
// BUTTON BOTTOM LEFT ??? Here is the code that does not work
function mouseDownResizeElementResizerBottomLeft(e) {
window.addEventListener("mousemove", mouseMoveResizeElementResizerBottomLeft, true);
document.getElementById("dragdiv").addEventListener('mouseup', mouseUpResizeElementResizerBottomLeft, true);
originaldivwidth = document.getElementById("dragdiv").style.width;
originaldivleft = document.getElementById("dragdiv").getBoundingClientRect().left;
originaldivright = document.getElementById("dragdiv").getBoundingClientRect().right;
originalmousex = e.clientX;
}
function mouseMoveResizeElementResizerBottomLeft(e) {
document.getElementById("dragdiv").addEventListener('mouseup', mouseUpResizeElementResizerBottomLeft, true);
window.removeEventListener("mousemove", myFunctionDragElement, true);
var elementoriginalwidth = document.getElementById("dragdiv").style.width;
var elementoriginalleft = document.getElementById("dragdiv").getBoundingClientRect().left;
var mouseX = e.clientX;
document.getElementById("dragdiv").style.width = document.getElementById("dragdiv").style.width + (e.clientX - originalmousex) + 'px';
document.getElementById("dragdiv").style.left = e.clientX + (e.clientX - originalmousex) + 'px';
}
function mouseUpResizeElementResizerBottomLeft() {
window.removeEventListener("mousemove", mouseMoveResizeElementResizerBottomLeft, true);
}
#dragdiv {
width: 100px;
height: 100px;
background-color: red;
position: absolute;
margin: 0px;
}
#resizer-top-left {
width: 10px;
height: 10px;
border-radius: 50%;
background: white;
border: 3px solid #4286f4;
position: absolute;
left: -5px;
top: -5px;
cursor: nwse-resize;
}
#resizer-top-right {
width: 10px;
height: 10px;
border-radius: 50%;
background: white;
border: 3px solid #4286f4;
position: absolute;
right: -5px;
top: -5px;
cursor: nesw-resize;
}
#resizer-bottom-left {
width: 10px;
height: 10px;
border-radius: 50%;
background: white;
border: 3px solid #4286f4;
position: absolute;
left: -5px;
bottom: -5px;
cursor: nesw-resize;
}
#resizer-bottom-right {
width: 10px;
height: 10px;
border-radius: 50%;
background: white;
border: 3px solid #4286f4;
position: absolute;
right: -5px;
bottom: -5px;
cursor: nwse-resize;
}
<div id="container">
<div id="dragdiv"></div>
</div>