使用js实现拖拽效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
width: 200px;
height: 200px;
background-color: rgb(22, 240, 240);
position: absolute;
left: 0;
top: 0;
}
</style>
</head>
<body>
<div class="box">
</div>
<script>
let box=document.querySelector('.box')
let ismove=false;
let offset={};
box.addEventListener('mousedown',(e)=>{
offset={
x:e.offsetX,
y:e.offsetY,
}
ismove=true
});
document.addEventListener('mousemove',(e)=>{
if(ismove){
box.style.left=e.pageX-offset.x+'px'
box.style.top=e.pageY-offset.y+'px'
}
})
document.addEventListener('mouseup',()=>{
ismove=false
})
</script>
</body>
</html>