js 左侧多个拖拽内容,拖拽其中一个dom到右侧,左侧dom不减少,右侧dom判断拖拽到dom内,未释放前可进行排序,右侧dom有多层结构,并且每层有个可以拖拽的按钮,鼠标移动上去即可拖拽排序并添加移动动画,请用完整class 类代码完整实现,并放在一个完整的html中,不使用第三方插件
时间: 2024-02-01 20:04:31 浏览: 60
好的,以下是一个简单的示例代码,实现了左右两侧的拖拽和排序功能:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Drag and Drop Demo</title>
<style>
.container {
display: flex;
justify-content: space-between;
height: 300px;
border: 1px solid #ccc;
padding: 10px;
}
.left {
width: 40%;
height: 100%;
border: 1px solid #ccc;
padding: 10px;
overflow-y: auto;
}
.right {
width: 40%;
height: 100%;
border: 1px solid #ccc;
padding: 10px;
overflow-y: auto;
}
.item {
cursor: move;
margin-bottom: 10px;
padding: 10px;
border: 1px solid #ccc;
background-color: #fff;
}
.item:hover {
background-color: #f0f0f0;
}
.placeholder {
border: 2px dashed #ccc;
height: 100%;
}
.button {
cursor: move;
margin-bottom: 10px;
padding: 10px;
border: 1px solid #ccc;
background-color: #fff;
text-align: center;
}
.button:hover {
background-color: #f0f0f0;
}
</style>
</head>
<body>
<div class="container">
<div class="left">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
<div class="item">Item 4</div>
</div>
<div class="right">
<div class="placeholder"></div>
</div>
</div>
<script>
// 左侧拖拽
const leftItems = document.querySelectorAll('.left .item');
leftItems.forEach(item => {
item.draggable = true;
item.addEventListener('dragstart', function(e) {
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setData('text/plain', null);
e.dataTransfer.setDragImage(e.target, 0, 0);
});
});
// 右侧拖放
const placeholder = document.querySelector('.right .placeholder');
let activeItem = null;
function setActiveItem(item) {
if (activeItem) {
activeItem.classList.remove('active');
}
activeItem = item;
if (activeItem) {
activeItem.classList.add('active');
}
}
placeholder.addEventListener('dragover', function(e) {
e.preventDefault();
e.dataTransfer.dropEffect = 'move';
setActiveItem(null);
const rect = placeholder.getBoundingClientRect();
const y = e.clientY - rect.top;
const height = placeholder.clientHeight;
const middle = height / 2;
if (y < middle) {
placeholder.parentElement.insertBefore(placeholder, placeholder.previousSibling);
} else {
placeholder.parentElement.insertBefore(placeholder, placeholder.nextSibling);
}
});
placeholder.addEventListener('dragenter', function(e) {
setActiveItem(placeholder);
});
placeholder.addEventListener('dragleave', function(e) {
setActiveItem(null);
});
placeholder.addEventListener('drop', function(e) {
e.preventDefault();
setActiveItem(null);
const item = document.createElement('div');
item.className = 'item';
item.textContent = e.dataTransfer.getData('text/plain');
placeholder.parentElement.insertBefore(item, placeholder);
});
// 右侧排序
const rightItems = document.querySelectorAll('.right .item');
rightItems.forEach(item => {
item.draggable = true;
item.addEventListener('dragstart', function(e) {
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setData('text/plain', null);
e.dataTransfer.setDragImage(e.target, 0, 0);
setActiveItem(item);
});
item.addEventListener('dragend', function(e) {
setActiveItem(null);
});
});
// 拖拽按钮
const buttons = document.querySelectorAll('.right .button');
buttons.forEach(button => {
button.draggable = true;
button.addEventListener('dragstart', function(e) {
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setData('text/plain', null);
e.dataTransfer.setDragImage(e.target, 0, 0);
setActiveItem(button);
});
button.addEventListener('dragend', function(e) {
setActiveItem(null);
});
});
</script>
</body>
</html>
```
这段代码中包含了左右两个区域,左侧区域包含了多个可拖拽的 item,右侧区域开始只有一个占位符,当拖拽 item 到右侧区域时会在占位符前插入一个新的 item。右侧区域的 item 和按钮都可以拖拽排序,拖拽按钮时会移动整个 item。同时,拖拽过程中也会显示拖拽的效果。
阅读全文