利用JavaScript实现拖拽功能,核心是通过监听鼠标的三大事件,动态计算目标元素的位置偏移,让元素跟随鼠标移动。整个实现过程不需要依赖第三方库,原生JavaScript就可以完成。

拖拽功能的核心实现逻辑
拖拽的完整流程分为三个步骤,每个步骤对应不同的鼠标事件:
- 鼠标按下(mousedown):记录鼠标按下时的初始位置,以及目标元素的初始偏移位置,同时标记拖拽状态为开启
- 鼠标移动(mousemove):当拖拽状态开启时,计算鼠标移动的距离,更新目标元素的位置,让元素跟随鼠标移动
- 鼠标松开(mouseup):关闭拖拽状态,移除鼠标移动事件的监听,结束拖拽流程
基础单元素拖拽实现
下面是一个可拖拽的div元素的基础实现代码,元素可以在页面内自由移动:
// 获取需要拖拽的元素
const dragElement = document.getElementById('dragBox');
// 定义变量存储初始状态
let isDragging = false;
let startX = 0;
let startY = 0;
let initialLeft = 0;
let initialTop = 0;
// 监听鼠标按下事件
dragElement.addEventListener('mousedown', function(e) {
isDragging = true;
// 记录鼠标按下时的坐标
startX = e.clientX;
startY = e.clientY;
// 记录元素当前的left和top值,如果元素没有设置定位,默认取0
initialLeft = parseInt(window.getComputedStyle(dragElement).left) || 0;
initialTop = parseInt(window.getComputedStyle(dragElement).top) || 0;
// 给元素添加激活样式,可选
dragElement.style.opacity = '0.8';
});
// 监听鼠标移动事件,绑定在document上避免鼠标移动过快脱离元素
document.addEventListener('mousemove', function(e) {
if (!isDragging) return;
// 计算鼠标移动的距离
const deltaX = e.clientX - startX;
const deltaY = e.clientY - startY;
// 更新元素的位置
dragElement.style.left = initialLeft + deltaX + 'px';
dragElement.style.top = initialTop + deltaY + 'px';
});
// 监听鼠标松开事件
document.addEventListener('mouseup', function() {
if (!isDragging) return;
isDragging = false;
// 恢复元素样式
dragElement.style.opacity = '1';
});
对应的HTML结构需要给拖拽元素设置定位属性,否则left和top不会生效:
<div id="dragBox" style="width: 100px; height: 100px; background: #409eff; position: absolute; cursor: move; color: white; text-align: center; line-height: 100px;">
可拖拽元素
</div>
拖拽边界限制实现
基础拖拽可能会让元素移出可视区域,我们可以添加边界限制,让元素只能在指定范围内移动:
// 获取拖拽元素和容器(如果没有容器则使用可视区域)
const dragElement = document.getElementById('dragBox');
const container = document.getElementById('container') || document.documentElement;
let isDragging = false;
let startX = 0;
let startY = 0;
let initialLeft = 0;
let initialTop = 0;
// 获取容器的边界范围
function getContainerBounds() {
if (container === document.documentElement) {
return {
minLeft: 0,
minTop: 0,
maxLeft: window.innerWidth - dragElement.offsetWidth,
maxTop: window.innerHeight - dragElement.offsetHeight
};
} else {
const containerRect = container.getBoundingClientRect();
return {
minLeft: 0,
minTop: 0,
maxLeft: containerRect.width - dragElement.offsetWidth,
maxTop: containerRect.height - dragElement.offsetHeight
};
}
}
dragElement.addEventListener('mousedown', function(e) {
isDragging = true;
startX = e.clientX;
startY = e.clientY;
initialLeft = parseInt(window.getComputedStyle(dragElement).left) || 0;
initialTop = parseInt(window.getComputedStyle(dragElement).top) || 0;
dragElement.style.opacity = '0.8';
});
document.addEventListener('mousemove', function(e) {
if (!isDragging) return;
const deltaX = e.clientX - startX;
const deltaY = e.clientY - startY;
// 计算临时位置
let newLeft = initialLeft + deltaX;
let newTop = initialTop + deltaY;
// 获取边界范围
const bounds = getContainerBounds();
// 限制位置在边界内
newLeft = Math.max(bounds.minLeft, Math.min(newLeft, bounds.maxLeft));
newTop = Math.max(bounds.minTop, Math.min(newTop, bounds.maxTop));
// 更新位置
dragElement.style.left = newLeft + 'px';
dragElement.style.top = newTop + 'px';
});
document.addEventListener('mouseup', function() {
if (!isDragging) return;
isDragging = false;
dragElement.style.opacity = '1';
});
多元素拖拽实现
如果需要页面内多个元素都支持拖拽,只需要给每个元素绑定相同的事件逻辑即可,不需要重复编写代码:
// 获取所有需要拖拽的元素,假设类名是draggable
const draggableElements = document.querySelectorAll('.draggable');
draggableElements.forEach(function(element) {
let isDragging = false;
let startX = 0;
let startY = 0;
let initialLeft = 0;
let initialTop = 0;
element.addEventListener('mousedown', function(e) {
isDragging = true;
startX = e.clientX;
startY = e.clientY;
initialLeft = parseInt(window.getComputedStyle(element).left) || 0;
initialTop = parseInt(window.getComputedStyle(element).top) || 0;
element.style.opacity = '0.8';
// 提升当前拖拽元素的层级,避免被其他元素遮挡
element.style.zIndex = '999';
});
document.addEventListener('mousemove', function(e) {
if (!isDragging) return;
const deltaX = e.clientX - startX;
const deltaY = e.clientY - startY;
element.style.left = initialLeft + deltaX + 'px';
element.style.top = initialTop + deltaY + 'px';
});
document.addEventListener('mouseup', function() {
if (!isDragging) return;
isDragging = false;
element.style.opacity = '1';
element.style.zIndex = '';
});
});
对应的多元素HTML结构示例:
<div class="draggable" style="width: 80px; height: 80px; background: #67c23a; position: absolute; left: 50px; top: 50px; cursor: move; color: white; text-align: center; line-height: 80px;">
元素1
</div>
<div class="draggable" style="width: 80px; height: 80px; background: #e6a23c; position: absolute; left: 200px; top: 150px; cursor: move; color: white; text-align: center; line-height: 80px;">
元素2
</div>
注意事项
- 拖拽元素的定位属性必须设置为absolute或者fixed,否则left和top属性不会生效
- 鼠标移动事件建议绑定在document上,避免鼠标移动速度过快脱离拖拽元素,导致拖拽中断
- 如果页面有文本选中需求,拖拽时可以给document添加user-select:none样式,避免拖拽时误选文本
- 移动端实现拖拽需要替换对应的触摸事件touchstart、touchmove、touchend,逻辑和鼠标事件类似
JavaScript拖拽功能事件监听mousedownmousemove修改时间:2026-07-06 13:21:37