<HTML>
<HEAD>
<TITLE>New
Document </TITLE>
<script
Language="javascript">
var x,y,divLeft,divTop;
var isMove = false;
var html = "";
function MouseDown(obj)
{
isMove = true;
x = event.clientX;
y = event.clientY;
divLeft = obj.offsetLeft;
divTop = obj.offsetTop;
}
function MouseMove(obj)
{
if(isMove)
{
obj.style.left = divLeft
+ event.clientX - x;
obj.style.top = divTop
+ event.clientY - y;
}
}
function MouseUp(obj)
{
isMove = false;
var a = parseInt(document.getElementById("div1").style.left);
var b = parseInt(document.getElementById("div1").style.top);
var c = parseInt(document.getElementById("div2").style.left);
var d = parseInt(document.getElementById("div2").style.top);
html =" <div id = 'div1' style='border:1px solid gray;width:42px;height:39px;position:absolute;left:"+a+"px;top:"+b+"px' onmousedown='MouseDown(this)' onmousemove='MouseMove(this)' onmouseup='MouseUp(this)'>a </div>";
line(a,b,c,d,"0000dd");
html += " <div id = 'div2' style='border:1px solid gray;width:45px;height:44px;position:absolute;left:"+c+"px;top:"+d+"px' onmousedown='MouseDown(this)' onmousemove=' MouseMove(this)' onmouseup='MouseUp(this)'>a </div>";
document.body.innerHTML=html
}
function
a(x,y,color)
{
html +=" <img border='0' style='position:absolute;left:"+(x+20)+";top:"+(y+20)+";background-color:"+color+"'src='px.gif' width=1 height=1>";
}
function
line(x1,y1,x2,y2,color)
{
var
tmp
if(x1>=x2)
{
tmp=x1;
x1=x2;
x2=tmp;
tmp=y1;
y1=y2;
y2=tmp;
}
for(var i=x1;i <=x2;i++)
{
x =i;
y =(y2-y1)/(x2-x1)*(x-x1)+y1;
a(x,y,color);
}
}
function show()
{
html =" <div id = 'div1' style='border:1px solid gray;width:42px;height:39px;position:absolute;left:1px;top:1px' onmousedown='MouseDown(this)' onmousemove='MouseMove(this)' onmouseup='MouseUp(this)'>a </div>";
line(1,1,100,100,"0000dd");
html += " <div id = 'div2' style='border:1px solid gray;width:45px;height:44px;position:absolute;left:100px;top:100px' onmousedown='MouseDown(this)' onmousemove='MouseMove(this)' onmouseup='MouseUp(this)'>a </div>";
document.body.innerHTML=html;
}
</script>
</HEAD>
<body onload="show()">
</BODY>
</HTML>
谢谢foktime这位朋友!但你的例子还有个小问题,那就是拖动div的时候div移动不是很顺畅!
还有就是能不能在此基础上用js实现右键单击其中一个div弹出相应的菜单,而且用鼠标双击其中一个div触发一个事件呢?举个例子,把源码上传上来,谢了,本人等你的回复,呵呵!
[
本帖最后由 gu_tong2008 于 2009-9-23 10:26 编辑 ]