<!DOCTYPE html>
<html lang="en">
<style>
#box{
width:300px;
height:150px;
background-color:purple;
position:absolute;
left:100px;
top:50px;
}
</style>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="box" draggable="true"> </div>
<script>
const box=document.querySelector('#box');
//拖拽移动时
/* box.ondragover=function (e) {
this.style.cssText=`left:${e.clientX-100}px;top:${e.clientY-50}px`
}*/
//拖拽结束时
box.ondragend=function (e) {
this.style.cssText=`left:${e.clientX-100}px;top:${e.clientY-50}px`
}
</script>
</body>
</html>