console
let containWrap = null,
startX = 0, startY = 0, endX = 0, endY = 0
function handleMouseDown(event) {
startX = event.clientX
startY = event.clientY
endX = event.clientX
endY = event.clientY
document.body.addEventListener('mousemove', handleMouseMove)
document.body.addEventListener('mouseup', handleMouseUp)
console.log('startX | startY: ', startX, startY)
}
function handleMouseMove(event) {
endX = event.clientX;
endY = event.clientY;
console.log('endX | endY: ', endX, endY)
}
function handleMouseUp() {
document.body.removeEventListener('mousemove', handleMouseMove)
document.body.removeEventListener('mouseup', handleMouseUp)
}
document.addEventListener('mousedown', handleMouseDown)
window.onload = function () {
containWrap = document.getElementById('containWrap')
console.log('containWrap: ', containWrap)
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript 拖拽布局</title>
</head>
<body>
<div class="contain-wrap" id="containWrap">
</div>
</body>
</html>
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
.contain-wrap {
width: 100vw;
height: 100vh;
cursor: crosshair;
position: relative;
z-index: 3;
}
.box-wrap {
width: 0px;
height: 0px;
border: dashed 1px lightskyblue;
}
.handle-box-wrap {
width: 100%;
height: 100%;
cursor: move;
border: 1px solid lightpink;
position: relative;
z-index: 5;
}
.btn-resize {
display: block;
width: 8px;
height: 8px;
border: 1px solid lightpink;
position: absolute;
z-index: 7;
}
.n-resize {
cursor: n-resize;
left: 50%;
top: -4px;
margin-left: -4px;
}
.ne-resize {
cursor: ne-resize;
right: -4px;
top: -4px;
}
.e-resize {
cursor: e-resize;
right: -4px;
top: 50%;
margin-top: -4px;
}
.se-resize {
cursor: se-resize;
right: -4px;
bottom: -4px;
}
.s-resize {
cursor: s-resize;
left: 50%;
bottom: -4px;
margin-left: -4px;
}
.sw-resize {
cursor: sw-resize;
left: -4px;
bottom: -4px;
}
.w-resize {
cursor: w-resize;
left: -4px;
top: 50%;
margin-top: -4px;
}
.nw-resize {
cursor: nw-resize;
left: -4px;
top: -4px;
}