原生JS实现Div拖拽与拉伸功能完整示例
作者:木棉时间:2026-10-25 21:28:54浏览:533
本文通过HTML、CSS和JavaScript代码,详细演示了如何在前端实现Div元素的自由拖拽移动与四角及边缘拉伸功能。示例包含完整的DOM结构、样式定义及事件监听逻辑,适用于需要自定义交互组件的开发场景。

前言
本文介绍如何使用JavaScript实现Div元素的拖拽移动与尺寸拉伸功能。该功能实现相对基础,此处直接提供相关代码示例。
HTML结构
<div class="resize" data-key="drag">
<img src="../images/liya.jpg" alt="">
<div class="line line-n" data-key="n"></div>
<div class="line line-e" data-key="e"></div>
<div class="line line-s" data-key="s"></div>
<div class="line line-w" data-key="w"></div>
<div class="point point-n" data-key="n"></div>
<div class="point point-e" data-key="e"></div>
<div class="point point-s" data-key="s"></div>
<div class="point point-w" data-key="w"></div>
<div class="point point-ne" data-key="ne"></div>
<div class="point point-se" data-key="se"></div>
<div class="point point-sw" data-key="sw"></div>
<div class="point point-nw" data-key="nw"></div>
</div>
CSS样式
* {
margin: 0;
padding: 0;
}
.container {
height: 100vh;
background: #000;
overflow: hidden;
user-select: none;
}
.resize {
position: relative;
outline: 1px solid #1890ff;
touch-action: none;
}
img {
display: block;
width: 100%;
height: 100%;
pointer-events: none;
}
.line {
position: absolute;
}
.line-n {
left: 0;
top: -3px;
width: 100%;
height: 6px;
cursor: n-resize;
}
.line-e {
top: 0;
right: -3px;
width: 6px;
height: 100%;
cursor: e-resize;
}
.line-s {
left: 0;
bottom: -3px;
width: 100%;
height: 6px;
cursor: s-resize;
}
.line-w {
top: 0;
left: -3px;
width: 6px;
height: 100%;
cursor: w-resize;
}
.point {
position: absolute;
width: 6px;
height: 6px;
background: #1890ff;
}
.point-n {
top: -3px;
left: calc(50% - 3px);
cursor: n-resize;
}
.point-e {
right: -3px;
top: calc(50% - 3px);
cursor: e-resize;
}
.point-s {
bottom: -3px;
left: calc(50% - 3px);
cursor: s-resize;
}
.point-w {
left: -3px;
top: calc(50% - 3px);
cursor: w-resize;
}
.point-ne {
top: -3px;
right: -3px;
cursor: ne-resize;
}
.point-se {
bottom: -3px;
right: -3px;
cursor: se-resize;
}
.point-sw {
left: -3px;
bottom: -3px;
cursor: sw-resize;
}
.point-nw {
left: -3px;
top: -3px;
cursor: nw-resize;
}
JavaScript逻辑
// 获取dom
const box = document.querySelector('.resize');
// 声明全局变量
let width = 200, height = 160, minWidth = 100, minHeight = 80, isPointerdown = false,
x = (window.innerWidth - width) * 0.5, y = (window.innerHeight - height) * 0.5,
diff = { x: 0, y: 0 }, lastPointermove = { x: 0, y: 0 }, key = '', rect = null;
box.style.width = width + 'px';
box.style.height = height + 'px';
box.style.transform = 'translate3d(' + x + 'px, ' + y + 'px, 0px)';
const action = {
drag: function () {
x += diff.x;
y += diff.y;
},
n: function (e) {
if (rect.bottom - e.clientY > minHeight) {
height = rect.bottom - e.clientY;
y = e.clientY;
}
},
e: function (e) {
if (e.clientX - rect.left > minWidth) {
width = e.clientX - rect.left;
}
},
s: function (e) {
if (e.clientY - rect.top > minHeight) {
height = e.clientY - rect.top;
}
},
w: function (e) {
if (rect.right - e.clientX > minWidth) {
width = rect.right - e.clientX;
x = e.clientX;
}
},
ne: function (e) {
this.n(e);
this.e(e);
},
se: function (e) {
this.s(e);
this.e(e);
},
sw: function (e) {
this.s(e);
this.w(e);
},
nw: function (e) {
this.n(e);
this.w(e);
}
}
// 绑定事件
box.addEventListener('pointerdown', function (e) {
isPointerdown = true;
e.target.setPointerCapture(e.pointerId);
lastPointermove = { x: e.clientX, y: e.clientY };
key = e.target.dataset.key;
rect = box.getBoundingClientRect();
});
box.addEventListener('pointermove', function (e) {
if (isPointerdown) {
const current = { x: e.clientX, y: e.clientY };
diff.x = current.x - lastPointermove.x;
diff.y = current.y - lastPointermove.y;
lastPointermove = { x: current.x, y: current.y };
action[key](e);
box.style.width = width + 'px';
box.style.height = height + 'px';
box.style.transform = 'translate3d(' + x + 'px, ' + y + 'px, 0)';
e.preventDefault();
}
});
box.addEventListener('pointerup', function (e) {
if (isPointerdown) {
isPointerdown = false;
}
});
box.addEventListener('pointercancel', function (e) {
if (isPointerdown) {
isPointerdown = false;
}
});
在线演示地址:jsdemo.codeman.top/html/divRes…
以上是关于使用JavaScript实现Div拖拽与拉伸功能的完整示例介绍,如需了解更多相关技术细节,建议查阅本站其他相关文章。
作者最新文章
JavaScript怎么准确判断数据类型:typeof、instanceof与Object.prototype.toString
2026-11-05 17:09:21
JS中将数组和对象混合组装为JSON字符串
2026-11-05 16:41:42
JS运算符优先级与结合性的区别是什么?
2026-11-05 13:22:02
JS Map怎么只遍历Key或Value
2026-11-05 13:00:36
D3.js 与 ECharts 选型对比:灵活性 vs 开发效率
2026-11-05 09:45:01
