<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>窗口拖动</title>
<style>
.box{
width: 300px;
height: 300px;
background: purple;
position: absolute;
top: 0;
left: 0;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
<script>
let box1=document.querySelector(".box");
box1.onmousedown=function(event){
let x0=event.offsetX;
let y0=event.offsetY;
box1.onmousemove=function(event){
let x1=event.clientX;
let y1=event.clientY;
box1.style.left=x1-x0+"px";
box1.style.top=y1-y0+"px";
}
box1.onmouseup=function(){
box1.onmousemove=""
box1.onmouseup=""
}
}
</script>