问题遇到的现象和发生背景
js写拖拽div 只能实现拖拽一遍 第二遍松开鼠标后还是会跟着鼠标走
问题相关代码,请勿粘贴截图
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
#box1 {
height: 100px;
width: 100px;
background-color: red;
position: absolute;
}
</style>
<script>
window.onload = function () {
var box1 = document.getElementById("box1");
box1.onmousedown = function () {
document.onmousemove = function (event) {
var left = event.clientX;
var top = event.clientY;
box1.style.left = left + "px";
box1.style.top = top + "px";
};
};
document.onmouseup = function () {
document.onmousemove = null;
document.onmouseup = null;
};
};
</script>
</head>
<body>
<!--
onmousedown
onmouseclick
onmouseup
-->
<div id="box1"></div>
</body>
</html>