<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
position: relative;
height: 100px;
width: 100px;
border: 2px solid #333;
border-radius: 10px;
background-size: cover;
background-position: center;
margin: 10px;
display: inline-block;
}
.image {
height: 100px;
width: 100px;
background: url("sam.jpeg") center/cover no-repeat;
border: 2px solid red;
}
</style>
</head>
<body>
<div class="box"></div>
<div class="box"></div>
<div class="image" draggable="true"></div>
<script>
const boxes = document.querySelectorAll(".box");
const image = document.querySelector(".image");
// supaya image bisa di-drag
image.addEventListener("dragstart", (e) => {
e.dataTransfer.setData("text/plain", "dragged-image");
});
boxes.forEach((box) => {
box.addEventListener("dragover", (e) => {
e.preventDefault();
});
box.addEventListener("drop", (e) => {
e.preventDefault();
box.appendChild(image);
});
});
</script>
</body>
</html>
No comments:
Post a Comment