<style>
.box {
width: 200px;
height: 200px;
background-color: greenyellow;
position: fixed;
left: 0px;
top: 0px;
}
</style>
<body>
<div class="box" ></div>
<script>
var div = document.querySelector('div');
div.onmousedown = function (e) {
e = e || window.event;
var offsetX = e.offsetX;
var offsetY = e.offsetY;
document.onmousemove = function (ev) {
ev = ev || window.event;
var clientX=ev.clientX;
var clientY=ev.clientY;
div.style.left=clientX-offsetX+'px';
div.style.top=clientY-offsetY+'px';
}
}
div.onmouseup=function () {
document.onmousemove=null;
}
</script>
</body>
js--实现鼠标拖动
最新推荐文章于 2023-06-09 10:00:00 发布