JSFiddle - React, Tailwind, and code Playground
by od0201
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="styles.css" />
<title>Drag & Drop | Проект 2</title>
</head>
<body>
<div>
<div class="row">
<div class="col-header start">Начать</div>
<div class="col-header progress">В процессе</div>
<div class="col-header done">Готовы</div>
</div>
<div class="row">
<div class="placeholder">
<div class="item" draggable="true">Перетащи меня</div>
</div>
<div class="placeholder"></div>
<div class="placeholder"></div>
</div>
</div>
<script src="app.js"></script>
</body>
</html>
CSS
@import url('https://fonts.googleapis.com/css?family=Roboto&display=swap');
* {
box-sizing: border-box;
}
body {
font-family: 'Roboto', sans-serif;
background-color: #e5e5e5;
display: flex;
padding-top: 5rem;
justify-content: center;
overflow: hidden;
margin: 0;
}
.row {
display: flex;
width: 600px;
justify-content: space-between;
margin-bottom: 1rem;
}
.col-header {
width: 150px;
box-shadow: 4px 4px 9px rgba(198, 198, 198, 0.36);
border-radius: 20px;
padding: 0.8rem 1rem;
color: #fff;
}
.item {
width: 150px;
height: 66px;
border: 1px solid #eee;
box-shadow: 4px 4px 9px rgba(198, 198, 198, 0.36);
border-radius: 20px;
background: #f7f6f7;
padding: 0.8rem 1rem;
color: #828282;
text-align: center;
cursor: grab;
}
.item:active {
cursor: grabbing;
}
.placeholder {
width: 150px;
height: 66px;
}
.err {
width: 100%;
border-radius: 20px;
padding: 0.8rem 1rem;
margin: 2rem 0;
color: #fff;
background: #f55;
text-align: center;
font-size: small;
word-wrap: break-wordж
}
.start {
background: linear-gradient(90deg, #ff85e4 0%, #229efd 179.25%);
}
.progress {
background: linear-gradient(90deg, #209cff 0%, #68e0cf 100%);
}
.done {
background: linear-gradient(90deg, #84fab0 0%, #8fd3f4 100%);
}
.hold{
/* border: 5px solid rgb(208, 231, 163); */
background:#ffbdbd;
}
.hide{
/* display: none; */
opacity: .3;
}
.hovered{
border-radius: 20px;
border: 2px solid #8ED7ED;
}
JavaScript
const item=document.querySelector(".item")
const placeholders=document.querySelectorAll('.placeholder')
item.addEventListener('dragstart',dragstart)
item.addEventListener('dragend',dragend)
placeholders.forEach((placeholder)=>{
placeholder.addEventListener('dragover',dragover)
placeholder.addEventListener('dragenter',dragenter)
placeholder.addEventListener('dragleave',dragleave)
placeholder.addEventListener('drop',dragdrop)
})
function dragstart (event){
event.target.classList.add('hold')
setTimeout(()=>event.target.classList.add('hide'),0)
}
function dragend (event){
event.target.classList.remove('hold')
event.target.classList.remove('hide')
}
function dragover (event){
event.preventDefault()
}
function dragenter (event){
event.target.classList.add('hovered')
}
function dragleave (event){
event.target.classList.remove('hovered')
}
function dragdrop (event){
event.target.classList.remove('hovered')
if(!this.querySelector(".item")){
event.target.append(item)
}
event.target.append(item)
}