一、效果

屏幕截图 2025-04-22 210156.png
二、代码实现
···
<!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:500px;
height: 500px;
background-image: url(./images/微信图片_20250422204305.jpg);
position:absolute;
}
</style>
</head>
<body>
<div id="box"></div>
<script>
let box = document.querySelector("#box")
box.onmousedown = function(a){
let ox = a.offsetX
let oy = a.offsetY
document.onmousemove=function(b){
let px = b.clientX
let py = b.clientY
let mx = px - ox
let my = py - oy
box.style.left = mx + "px"
box.style.top = my +"px"
}
}
document.onmouseup = function(){
document.onmousemove = null;
}
</script>
</body>
</html>
···