<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.box {
border: 1px solid #d6d6d6;
position: absolute;
}
.left-border {
width: 4px;
height: 200px;
background-color: #00c0ff;
position: absolute;
cursor: w-resize;
}
.top-border {
width: 200px;
height: 4px;
background-color: #00c0ff;
position: absolute;
cursor: n-resize;
}
.right-border {
width: 4px;
height: 200px;
background-color: #00c0ff;
position: absolute;
cursor: e-resize;
}
.bottom-border {
width: 200px;
height: 4px;
background-color: #00c0ff;
position: absolute;
cursor: s-resize;
}
</style>
</head>
<body>
<div class="box" style="width:200px;height:200px;top:200px;left:600px;">
<div class="left-border" id="left" style="left:0;top:0;"></div>
<div class="top-border" id="top" style="left:0;top:0;"></div>
<div class="right-border" id="right" style="right:0;top:0;"></div>
<div class="bottom-border" id="bottom" style="left:0;bottom:0;"></div>
</div>
</body>
<script>
let u_left = document.getElementById('left')
let u_top = document.getElementById('top')
let u_right = document.getElementById('right')
let u_bottom = document.getElementById('bottom')
let currentX = 0, // 用来记录鼠标按下时的横坐标
currentY = 0, // 用来记录鼠标按下时的纵坐标
currentDivWidth = 0, // 鼠标按下时div的宽度
currentDivHeight = 0, // 鼠标按下时div的高度
currentDivX = 0, // 鼠标按下时div的横坐标
currentDivY = 0 // 鼠标按下时div的纵坐标
let currentCanvasDiv = null // 鼠标按下时存放要改变大小的div
let currentBorder = null // 鼠标按下时存放当前拖动的边
// 绑定鼠标按下事件
u_left.onmousedown = mouseDown
u_top.onmousedown = mouseDown
u_right.onmousedown = mouseDown
u_bottom.onmousedown = mouseDown
function mouseDown(event) {
currentX = event.clientX
currentY = event.clientY
const path = event.path || (event.composedPath && event.composedPath()) // 获取当前点击事件冒泡所经过的路径同时解决火狐的兼容问题
currentCanvasDiv = path[1]
currentBorder = path[0].id
currentDivWidth = parseInt(currentCanvasDiv.style.width)
currentDivHeight = parseInt(currentCanvasDiv.style.height)
currentDivX = parseInt(currentCanvasDiv.style.left)
currentDivY = parseInt(currentCanvasDiv.style.top)
document.addEventListener("mousemove", eval(currentBorder + 'Move'))
document.addEventListener("mouseup", mouseUp)
}
function mouseUp() {
document.removeEventListener("mousemove", eval(currentBorder + 'Move'))
document.removeEventListener("mouseup", mouseUp)
}
// 拖动左侧边
function leftMove(event) {
let _X = event.clientX
let _width = currentDivWidth + currentX - _X // 拖动后div的宽度
if (_width <= 200) { // 设置可以拖动的最小宽度
u_bottom.style.width = u_top.style.width = currentCanvasDiv.style.width = 200 + 'px'
currentCanvasDiv.style.left = currentDivWidth + currentDivX - 200 + 'px' // 使左侧边向左移动,否则改变宽度时div会默认向右增宽
} else {
u_bottom.style.width = u_top.style.width = currentCanvasDiv.style.width = _width + 'px'
currentCanvasDiv.style.left = currentDivX + _X - currentX + 'px'
}
}
function rightMove(event) {
let _X = event.clientX
let _width = currentDivWidth + _X - currentX
if (_width <= 200) {
u_bottom.style.width = u_top.style.width = currentCanvasDiv.style.width = 200 + 'px'
} else {
u_bottom.style.width = u_top.style.width = currentCanvasDiv.style.width = _width + 'px'
}
}
function bottomMove(event) {
let _Y = event.clientY
let _height = currentDivHeight + _Y - currentY
if (_height <= 200) {
u_left.style.height = u_right.style.height = currentCanvasDiv.style.height = 200 + 'px'
} else {
u_left.style.height = u_right.style.height = currentCanvasDiv.style.height = _height + 'px'
}
}
function topMove(event) {
let _Y = event.clientY
let _height = currentDivHeight + currentY - _Y
if (_height <= 200) {
u_left.style.height = u_right.style.height = currentCanvasDiv.style.height = 200 + 'px'
currentCanvasDiv.style.top = currentDivHeight + currentDivY - 200 + 'px'
} else {
u_left.style.height = u_right.style.height = currentCanvasDiv.style.height = _height + 'px'
currentCanvasDiv.style.top = currentDivY + _Y - currentY + 'px'
}
}
</script>
</html>
div拖拽改变大小的demo
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 背景设置(参考前边rem布局代码理解) .div{ position:absolute; width:10rem;...
- layout: post title: '如何实现一个可拖拽的圆并且可以改变大小' subtitle: '转载请注...