作业分析
本次使用div+css+Javascrip等标签编写出如下的效果:
JS实现鼠标拖曳最终演示图:

image.png
代码展示入下(使用vscode编辑)
html代码展示如下:
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#box {
width: 300px;
height: 200px;
background-color: red;
position: absolute;
}
</style>
</head>
<body>
<div id="box">
<h2>拖拽</h2>
</div>
<script>
// 获得要操作的标签对象
let box = document.getElementById('box')
// 事件1:鼠标位置1 按下,获得offsetX和offsetY
box.onmousedown = function (e) {
let ox = e.offsetX
let oy = e.offsetY
// 事件2:鼠标在网页中移动(包含在事件1中)
document.onmousemove = function (e2) {
let cx = e2.clientX
let cy = e2.clientY
// 计算left和top
let _left = cx - ox
let _top = cy - oy
// 边界判断
if (_left < 0) {
// 左边界
_left = 0
}
if (_top < 0) {
// 上边界
_top = 0
}
if (_left > document.documentElement.clientWidth - box.offsetWidth) {
_left = document.documentElement.clientWidth - box.offsetWidth
}
// 下边界
if (_top > document.documentElement.clientHeight - box.offsetHeight) {
_top = document.documentElement.clientHeight - box.offsetHeight
}
// 获得鼠标在浏览器窗口中的位置
box.style.left = _left + "px"
box.style.top = _top + "px"
}
}
// 事件3:鼠标位置2 抬起,停止移动
document.onmouseup = function () {
// 停止移动
document.onmousemove = null
}
</script>
</body>
</html>