JSFiddle - React, Tailwind, and code Playground
by Tolibjon Tolibov
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;
}
.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: 1px dashed rgb(38, 183, 228);
}
.hide {
display: none;
}
.hovered{
border: 1px solid #8ed7ed;
background: #ccc;
border-top: 2px solid #8ed7ed;
}
JavaScript
const item = document.querySelector('.item');
const placeholders = document.querySelectorAll('.placeholder');
item.addEventListener('dragstart', dragstart )
item.addEventListener('dragend', dragend)
for (const placeholder of placeholders) {
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.className='item';
}
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.append(item)
event.target.classList.remove('hovered')
}