js 左侧多个拖拽内容,拖拽其中一个dom到右侧,左侧dom不减少,右侧dom判断拖拽到dom内,未释放前可进行排序,右侧dom有多层结构,并且每层有个可以拖拽的按钮,鼠标移动上去即可拖拽排序并添加移动动画,请用完整class 类代码完整实现,并放在一个完整的html中,不使用第三方插件
时间: 2024-02-01 18:04:30 浏览: 64
好的,下面是完整的 HTML 和 CSS 代码,实现了左右两侧的拖拽和排序,以及右侧多层结构和拖拽按钮的移动排序效果:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>拖拽排序</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
display: flex;
justify-content: space-between;
margin-top: 20px;
}
.left {
width: 40%;
background-color: #eee;
padding: 20px;
}
.left .box {
margin-bottom: 10px;
cursor: move;
}
.right {
width: 55%;
background-color: #f2f2f2;
padding: 20px;
}
.right .layer {
position: relative;
padding-left: 30px;
margin-bottom: 10px;
}
.right .layer:before {
content: '';
position: absolute;
left: 0;
top: 50%;
transform: translateY(-50%);
width: 20px;
height: 1px;
background-color: #333;
}
.right .btn {
position: absolute;
top: 50%;
transform: translateY(-50%);
right: 0;
width: 30px;
height: 30px;
background-color: #333;
color: #fff;
text-align: center;
line-height: 30px;
cursor: move;
}
.active {
opacity: 0.5;
cursor: grabbing;
}
</style>
</head>
<body>
<div class="container">
<div class="left">
<div class="box">拖拽内容1</div>
<div class="box">拖拽内容2</div>
<div class="box">拖拽内容3</div>
<div class="box">拖拽内容4</div>
</div>
<div class="right">
<div class="layer">
<div class="btn">+</div>
层级1-1
</div>
<div class="layer">
<div class="btn">+</div>
层级1-2
<div class="layer">
<div class="btn">+</div>
层级2-1
</div>
<div class="layer">
<div class="btn">+</div>
层级2-2
</div>
</div>
<div class="layer">
<div class="btn">+</div>
层级1-3
</div>
</div>
</div>
<script>
var activeItem = null;
var sourceItems = document.querySelectorAll('.left .box');
var targetItems = document.querySelectorAll('.right .layer');
// 拖拽事件
function onDragStart(e) {
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setData('text/plain', null);
activeItem = this;
activeItem.classList.add('active');
}
function onDragOver(e) {
if (e.preventDefault) {
e.preventDefault();
}
e.dataTransfer.dropEffect = 'move';
return false;
}
function onDragEnter(e) {
this.classList.add('over');
}
function onDragLeave(e) {
this.classList.remove('over');
}
function onDrop(e) {
if (e.stopPropagation) {
e.stopPropagation();
}
if (activeItem !== this) {
this.appendChild(activeItem);
}
return false;
}
function onDragEnd(e) {
activeItem.classList.remove('active');
}
// 排序事件
function onBtnDragStart(e) {
e.stopPropagation();
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setData('text/plain', null);
activeItem = this.parentNode;
activeItem.classList.add('active');
}
function onBtnDragOver(e) {
e.preventDefault();
e.dataTransfer.dropEffect = 'move';
}
function onBtnDragEnter(e) {
this.parentNode.classList.add('over');
}
function onBtnDragLeave(e) {
this.parentNode.classList.remove('over');
}
function onBtnDrop(e) {
e.stopPropagation();
if (activeItem !== this.parentNode) {
this.parentNode.parentNode.insertBefore(activeItem, this.parentNode.nextSibling);
}
return false;
}
function onBtnDragEnd(e) {
activeItem.classList.remove('active');
}
// 绑定拖拽事件
for (var i = 0; i < sourceItems.length; i++) {
sourceItems[i].addEventListener('dragstart', onDragStart, false);
sourceItems[i].addEventListener('dragend', onDragEnd, false);
}
for (var i = 0; i < targetItems.length; i++) {
targetItems[i].addEventListener('dragover', onDragOver, false);
targetItems[i].addEventListener('dragenter', onDragEnter, false);
targetItems[i].addEventListener('dragleave', onDragLeave, false);
targetItems[i].addEventListener('drop', onDrop, false);
var btn = targetItems[i].querySelector('.btn');
btn.addEventListener('dragstart', onBtnDragStart, false);
btn.addEventListener('dragover', onBtnDragOver, false);
btn.addEventListener('dragenter', onBtnDragEnter, false);
btn.addEventListener('dragleave', onBtnDragLeave, false);
btn.addEventListener('drop', onBtnDrop, false);
btn.addEventListener('dragend', onBtnDragEnd, false);
}
</script>
</body>
</html>
```
这段代码中,我们使用了 HTML5 中的拖拽 API,包括 `dragstart`、`dragover`、`dragenter`、`dragleave`、`drop` 和 `dragend` 等事件,以及 `dataTransfer` 对象和 `effectAllowed` 和 `dropEffect` 属性来控制拖拽和放置的效果。同时,我们还使用了 CSS 的 `cursor` 属性和 `opacity` 属性来实现拖拽时的样式变化。请注意,这段代码还未做兼容性处理,建议在现代浏览器中运行。
阅读全文