js 左侧多个拖拽内容,拖拽其中一个dom到右侧,左侧dom不减少,右侧dom判断拖拽到dom内,未释放前可进行排序,右侧dom有多层结构,并且每层有个可以拖拽的按钮,鼠标移动上去即可拖拽排序并添加移动动画,请用完整class 类代码完整实现,并放在一个完整的html中,不使用第三方插件
时间: 2024-02-06 19:02:09 浏览: 85
好的,以下是完整的HTML和class类代码,实现了左侧多个dom拖拽到右侧进行排序和移动动画的功能:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>拖拽排序</title>
<style>
.container {
display: flex;
flex-wrap: wrap;
justify-content: space-around;
}
.item {
width: 100px;
height: 100px;
margin: 10px;
background-color: #ccc;
border: 1px solid #999;
border-radius: 5px;
text-align: center;
line-height: 100px;
cursor: move;
}
.target {
width: 200px;
height: 400px;
margin: 10px;
padding: 10px;
background-color: #eee;
border: 1px solid #999;
border-radius: 5px;
overflow: auto;
}
.inner {
width: 100%;
height: 100px;
margin: 10px 0;
padding: 10px;
background-color: #fff;
border: 1px solid #999;
border-radius: 5px;
display: flex;
align-items: center;
justify-content: space-between;
}
.handle {
width: 20px;
height: 20px;
background-color: #999;
border-radius: 50%;
cursor: move;
}
</style>
</head>
<body>
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
</div>
<div class="target">
<div class="inner">
<span>1</span>
<div class="handle"></div>
</div>
<div class="inner">
<span>2</span>
<div class="handle"></div>
</div>
<div class="inner">
<span>3</span>
<div class="handle"></div>
</div>
</div>
<script>
class Drag {
constructor(el) {
this.el = el;
this.dragging = false;
this.offsetX = 0;
this.offsetY = 0;
this.handleMouseDown = this.handleMouseDown.bind(this);
this.handleMouseMove = this.handleMouseMove.bind(this);
this.handleMouseUp = this.handleMouseUp.bind(this);
this.el.addEventListener('mousedown', this.handleMouseDown);
}
handleMouseDown(e) {
if (e.target.classList.contains('handle')) {
this.dragging = true;
this.offsetX = e.offsetX;
this.offsetY = e.offsetY;
this.el.style.position = 'absolute';
document.body.appendChild(this.el);
this.moveAt(e.pageX, e.pageY);
document.addEventListener('mousemove', this.handleMouseMove);
document.addEventListener('mouseup', this.handleMouseUp);
}
}
handleMouseMove(e) {
this.moveAt(e.pageX, e.pageY);
}
handleMouseUp(e) {
this.dragging = false;
document.removeEventListener('mousemove', this.handleMouseMove);
document.removeEventListener('mouseup', this.handleMouseUp);
const target = document.elementFromPoint(e.clientX, e.clientY);
if (target && target.classList.contains('inner')) {
target.appendChild(this.el);
this.el.style.position = 'static';
}
}
moveAt(x, y) {
this.el.style.left = x - this.offsetX + 'px';
this.el.style.top = y - this.offsetY + 'px';
}
}
// 拖拽左侧的项目
const items = document.querySelectorAll('.item');
items.forEach(item => {
new Drag(item);
});
// 拖拽右侧的按钮和内部的项目
const inners = document.querySelectorAll('.inner');
inners.forEach(inner => {
new Drag(inner.querySelector('.handle'));
const items = inner.querySelectorAll('.item');
items.forEach(item => {
new Drag(item);
});
});
</script>
</body>
</html>
```
在上面的代码中,我们定义了一个`Drag`类来实现拖拽和移动动画的功能。在构造函数中,我们传入要拖拽的元素`el`,并初始化一些属性,如`dragging`表示是否正在拖拽,`offsetX`和`offsetY`表示鼠标在被拖拽元素内的偏移量。
然后,我们在构造函数中绑定了三个事件处理函数:`handleMouseDown`、`handleMouseMove`和`handleMouseUp`。当鼠标在被拖拽元素上按下时,会触发`handleMouseDown`,判断是否按下了拖拽的按钮,然后设置一些属性并绑定`mousemove`和`mouseup`事件。在`handleMouseMove`中,我们调用`moveAt`函数来移动被拖拽元素的位置。在`handleMouseUp`中,我们判断鼠标释放时是否在目标元素内,并将被拖拽元素添加到目标元素内,然后重置一些属性。
在最后的代码中,我们首先选择了左侧和右侧的元素,并分别为其中的按钮和项目添加了`Drag`实例来实现拖拽和移动动画的功能。
阅读全文