
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>完美拖拽</title>
<style type="text/css">
html,
body {
overflow: hidden;
}
body,
div,
h2,
p {
margin: 0;
padding: 0;
}
body {
color: #fff;
background: #000;
font: 12px/2 Arial;
}
p {
padding: 0 10px;
margin-top: 10px;
}
span {
color: #ff0;
padding-left: 5px;
}
#box {
position: absolute;
width: 300px;
height: 150px;
background: #333;
border: 2px solid #ccc;
top: 50%;
left: 50%;
margin: -75px 0 0 -150px;
}
#box h2 {
height: 25px;
cursor: move;
background: #222;
border-bottom: 2px solid #ccc;
text-align: right;
padding: 0 10px;
}
#box h2 a {
color: #fff;
font: 12px/25px Arial;
text-decoration: none;
outline: none;
}
</style>
<script type="text/javascript">
window.onload = function() {
var oBox = document.getElementById("box");
var oH2 = oBox.getElementsByTagName("h2")[0];
var oA = oBox.getElementsByTagName("a")[0];
var aSpan = oBox.getElementsByTagName("span");
var disX = disY = 0;
var bDrag = false;
var aPos = [{
x: oBox.offsetLeft,
y: oBox.offsetTop
}]
oH2.onmousedown = function(event) {
var event = event || window.event;
bDrag = true;
disX = event.clientX - oBox.offsetLeft;
disY = event.clientY - oBox.offsetTop;
aPos.push({
x: oBox.offsetLeft,
y: oBox.offsetTop
})
return false
};
document.onmousemove = function(event) {
if (!bDrag) return;
var event = event || window.event;
var iL = event.clientX - disX;
var iT = event.clientY - disY;
var maxL = document.documentElement.clientWidth - oBox.offsetWidth;
var maxT = document.documentElement.clientHeight - oBox.offsetHeight;
iL = iL < 0 ? 0 : iL;
iL = iL > maxL ? maxL : iL;
iT = iT < 0 ? 0 : iT;
iT = iT > maxT ? maxT : iT;
oBox.style.marginTop = oBox.style.marginLeft = 0;
oBox.style.left = iL + "px";
oBox.style.top = iT + "px";
aPos.push({
x: iL,
y: iT
})
status();
return false
};
document.onmouseup = window.onblur = function() {
bDrag = false;
oH2.releaseCapture && oH2.releaseCapture();
status()
};
oA.onclick = function() {
if (aPos.length == 1) return;
var timer = setInterval(function() {
var oPos = aPos.pop();
oPos ? (oBox.style.left = oPos.x + "px", oBox.style.top = oPos.y + "px", status()) : clearInterval(timer)
}, 30);
return false
};
oA.onmousedown = function(event) {
(event || window.event).cancelBubble = true
};
function status() {
aSpan[0].innerHTML = bDrag;
aSpan[1].innerHTML = oBox.offsetTop;
aSpan[2].innerHTML = oBox.offsetLeft
}
status()
};
</script>
</head>
<body>
<div id="box">
<h2><a href="javascript:;">点击回放拖动轨迹</a></h2>
<p><strong>Drag:</strong><span></span></p>
<p><strong>offsetTop:</strong><span></span></p>
<p><strong>offsetLeft:</strong><span></span></p>
</div>
</body>
</html>
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库