做一个可移动的div
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
*{
margin: 0;
padding: 0;
}
div{
width: 100px;
height: 100px;
background-color: rebeccapurple;
}
</style>
</head>
<body>
<div></div>
<script>
let div=document.querySelector("div")
div.style.position="absolute"
w=parseInt(getComputedStyle(div).width)
h=parseInt(getComputedStyle(div).height)
div.onmousedown=function(event){
div.onmousemove=function(ev){
console.log(ev.offsetX)
console.log(ev.offsetY)
div.style.left=(ev.pageX-w/2)+"px"
div.style.top=(ev.pageX-h/2)+"px"
}
}
div.onmouseup=function(){
div.onmousemove=null
}
</script>
</body>
</html>