怎样用javascript实现两个div之间有连线,并且可以拖动?
不知道有没有高手可以用javascript实现两个div之间有连线,并且拖动任一个div其连线可以自由伸缩?能否举个例子?把源码上传上来看看,急,在线等。
<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>网上搜的。