var drag_ = false
var D = new Function('obj', 'return document.getElementById(obj);')
var oevent = new Function('e', 'if (!e) e = window.event;return e')
function Move_obj(obj) {
var x, y;
D(obj).onmousedown = function (e) {
drag_ = true;
with (this) {
style.position = "absolute"; var temp1 = offsetLeft; var temp2 = offsetTop;
x = oevent(e).clientX; y = oevent(e).clientY;
document.onmousemove = function (e) {
if (!drag_) return false;
with (this) {
style.left = temp1 + oevent(e).clientX - x + "px";
style.top = temp2 + oevent(e).clientY - y + "px";
}
}
}
document.onmouseup = new Function("drag_=false");
}
}
<div onmousedown="Move_obj(this.id)" id="dd" style="width:100px;height:100px;background:red"></div>
如果需要以后都留在此位置,需把当前位置的坐标保存到数据库,下次打开时读取数据加载div
追问这是鼠标拖动效果,
我想要的是div跟随鼠标移动,单击后,鼠标离开,div不再跟随移动,div停留在鼠标单击的位置,鼠标可以随意移动
追答差不多吧,你拖运到你想要的位置之后鼠标放开就行了
差不多吧,你拖运到你想要的位置之后鼠标放开就行了