티스토리 뷰

 
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<link rel="stylesheet" href="./css/style.css">
</head>
<body>
	
<div class="empty">
	<div class="fill" draggable="true"></div>
</div>
<div class="empty"></div>
<div class="empty"></div>
<div class="empty"></div>
<div class="empty"></div>



	<script src="js/main.js"></script>
</body>
</html>
body{
 background:darksalmon;
}
.fill{
	background-image:url('../img/img_01.jpg');
	position: relative;
	height: 150px; width: 150px;
	top: 5px; left: 5px;
	cursor:pointer;
}
.empty{
	display: inline-block;
	height: 160px; width: 160px;
	margin: 10px;
	border:3px solid salmon;
	background-color: #fff;
}
.hold{
	border:solid #ccc 4px;
}
.hovered{
	background: #f4f4f4;
	border-style: dashed;
}
.invisible{
display: none;
}
const fill = document.querySelector('.fill');
const empties = document.querySelectorAll('.empty');

//fill listeners
fill.addEventListener('dragstart', dragStart);
fill.addEventListener('dragend', dragEnd);

//Loop through empties and call drag events
for(const empty of empties){
	empty.addEventListener('dragover', dragOver);
	empty.addEventListener('dragenter', dragEnter);
	empty.addEventListener('dragleave', dragLeave);
	empty.addEventListener('drop', dragDrop);
}

//drag function

function dragStart(){
	this.className += ' hold';
	setTimeout(() =>(this.className = 'invisible'), 0);
    console.log('dragstart');
}

function dragEnd(){
	this.className = 'fill';
    console.log('dragend');
}

function dragOver(e){
	e.preventDefault();
    console.log('over');
}

function dragEnter(e){
	e.preventDefault();
    console.log('enter')
	this.className += ' hovered';
}

function dragLeave(){
	this.className = 'empty';
    console.log('leave');
}

function dragDrop(){
	this.className = 'empty';
	this.append(fill);
    
}

 

드래그 전

 

드래그 시작 및 다른 영역에 호버되었을시

 

드래그 종료

https://youtu.be/C22hQKE_32c

위 강의를 보고그대로 만듬.. 물론 몰라서 콘솔로그는 계속 찍어봄

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
글 보관함