加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
dragdrop.html 1.30 KB
一键复制 编辑 原始数据 按行查看 历史
jwyuef 提交于 2014-12-09 21:21 . update
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="box"> </div>
<script type="text/javascript">
var box = document.getElementById('box');
box.style.position = "absolute";
box.style.width = "160px";
box.style.height = "160px";
box.style.backgroundColor = "red";
var mx, my, ox, oy;
function e(event) {
if (!event) {
event = window.event;
event.target = event.srcElement;
event.layerX = event.offsetX;
event.layerY = event.offsetY;
}
event.mx = event.pageX || event.clientX + document.body.scrollLeft;
event.my = event.pageY || event.clientY + document.body.scrollTop;
return event;
}
document.onmousedown = function(event) {
event = e(event);
o = event.target;
ox = parseInt(o.offsetLeft);
oy = parseInt(o.offsetTop);
mx = event.mx;
my = event.my;
document.onmousemove = move;
document.onmouseup = stop;
}
function move(event) {
event = e(event);
o.style.left = ox+event.mx-mx + "px";
o.style.top = oy+event.my-my + "px";
}
function stop = function() {
event = e(event);
ox = parseInt(o.offsetLeft);
oy = parseInt(o.offsetTop);
mx = event.mx;
my = event.my;
o = document.onmousemove = document.onmouseup = null;
}
</script>
</body>
</html>
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化