<!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.οndragοver=function (e) {
this.style.cssText=`left:${e.clientX-100}px;top:${e.clientY-50}px`
}*/
//拖拽结束时
box.οndragend=function (e) {
this.style.cssText=`left:${e.clientX-100}px;top:${e.clientY-50}px`
}
</script>
</body>
</html>
转载于:https://www.cnblogs.com/yuesebote/p/10952620.html