JavaScript Realizes Arbitrary Scaling of Rectangular Block Size
- 2021-08-06 20:54:56
- OfStack
Recently wrote a native JavaScript rectangular block size to achieve arbitrary scaling of the case, feel the things inside the comparison around, here to share the source code for everyone, 1 learning 1 under.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> Native JavaScript Realize arbitrary scaling of rectangular block size </title>
</head>
<style>
* {
margin: 0;
padding: 0;
}
.box {
position: relative;
top: 200px;
left: 500px;
width: 500px;
height: 300px;
background-color: blueviolet;
}
.box>div:nth-child(-n+4) {
position: absolute;
width: 5px;
height: 5px;
background-color: #cc00ff;
}
.box>div:nth-child(n+5) {
position: absolute;
background-color: #f30497;
}
.box>.dot1 {
cursor: nw-resize;
top: -5px;
left: -5px;
}
.box>.dot2 {
cursor: ne-resize;
top: -5px;
right: -5px;
}
.box>.dot3 {
cursor: se-resize;
bottom: -5px;
right: -5px;
}
.box>.dot4 {
cursor: sw-resize;
bottom: -5px;
left: -5px;
}
.box>.line1,
.box>.line3 {
width: 500px;
height: 5px;
}
.box>.line1 {
top: -5px;
cursor: n-resize;
}
.box>.line3 {
bottom: -5px;
cursor: s-resize;
}
.box>.line2,
.box>.line4 {
width: 5px;
height: 300px;
}
.box>.line2 {
right: -5px;
cursor: e-resize;
}
.box>.line4 {
left: -5px;
cursor: e-resize;
}
</style>
<body>
<div class="box">
<div class="dot1"></div>
<div class="dot2"></div>
<div class="dot3"></div>
<div class="dot4"></div>
<div class="line1"></div>
<div class="line2"></div>
<div class="line3"></div>
<div class="line4"></div>
</div>
<script>
// Get related element nodes
let box = document.querySelector(".box");
let dots = document.querySelectorAll(".box>div:nth-child(-n+4)");
let lines = document.querySelectorAll(".box>div:nth-child(n+5)");
// Class border width
let bd = 5;
//for Loop to specify event objects
function changeBox([box, dots, lines, bd]) {
for (let i = 0; i < 4; i++) {
let { 0: dot1, 1: dot2, 2: dot3, 3: dot4 } = dots; //dots Object deconstruction
let { 0: line1, 1: line2, 2: line3, 3: line4 } = lines; //lines Object deconstruction
// Rectangular vertices click and drag to resize
dots[i].onmousedown = function (ev) {
// Click on the event to initialize the relevant value
let oldY = ev.clientY; // Vertex original y Value
let oldX = ev.clientX; // Vertex original x Value
let h = box.clientHeight; //box The height of
let w = box.clientWidth; //box Width of
let t = box.offsetTop; //box Distance from the top top Value
let l = box.offsetLeft; //box Distance to the left left Value
window.onmousemove = function (e) {
let offsetY = e.clientY - oldY; // Mouse movement y Axis offset
let offsetX = e.clientX - oldX; // Mouse movement x Axis offset
//if The condition is to determine the node target object clicked by the mouse
if (i == 0) {
box.style.height = `${h - offsetY + bd}px`; //box Altitude change
box.style.top = `${t + offsetY - bd}px`; //box Adj. top Value change ( Positioning )
box.style.width = `${w - offsetX + bd}px`; //box Width change of
box.style.left = `${l + offsetX - bd}px`; //box Adj. left Value change ( Positioning )
} else if (i == 1) {
box.style.height = `${h - offsetY + bd}px`;
box.style.top = `${t + offsetY - bd}px`;
box.style.width = `${w + offsetX + bd}px`;
} else if (i == 2) {
box.style.height = `${h + offsetY + bd}px`;
box.style.width = `${w + offsetX + bd}px`;
} else if (i == 3) {
box.style.height = `${h + offsetY + bd}px`;
box.style.width = `${w - offsetX + bd}px`;
box.style.left = `${l + offsetX - bd}px`;
}
// Height of border line / Width follows box The size changes and changes
line1.style.width = box.style.width;
line2.style.height = box.style.height;
line3.style.width = box.style.width;
line4.style.height = box.style.height;
}
}
// Click and drag the border to resize
lines[i].onmousedown = function (ev) {
// Click on the event to initialize the relevant value
let oldY = ev.clientY; // Vertex original y Value
let oldX = ev.clientX; // Vertex original x Value
let h = box.clientHeight; //box The height of
let w = box.clientWidth; //box Width of
let t = box.offsetTop; //box Distance from the top top Value
let l = box.offsetLeft; //box Distance to the left left Value
window.onmousemove = function (e) {
let offsetX = e.clientX - oldX; // Mouse movement x Axis offset
let offsetY = e.clientY - oldY; // Mouse movement y Axis offset
//if The condition is to determine the node target object clicked by the mouse
if (i == 0) {
box.style.height = `${h - offsetY + bd}px`;
box.style.top = `${t + offsetY - bd}px`;
} else if (i == 1) {
box.style.width = `${w + offsetX + bd}px`;
} else if (i == 2) {
box.style.height = `${h + offsetY + bd}px`;
} else {
box.style.width = `${w - offsetX + bd}px`;
box.style.left = `${l + offsetX - bd}px`;
}
// Height of border line / Width follows box The size changes and changes
line1.style.width = box.style.width;
line2.style.height = box.style.height;
line3.style.width = box.style.width;
line4.style.height = box.style.height;
}
}
// Clear the mouse movement event after the mouse is lifted
window.onmouseup = function () {
window.onmousemove = false;
}
}
}
changeBox([box, dots, lines, bd])
</script>
</body>
</html>