div 拖动 移动
源代码如下:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="content-type"content="text/html;charset=gb2312"/>
<metahttp-equiv="content-language"content="zh-cn"/>
<title>安博中程企业培训,企业内训,专注于android培训,软考培训,软件开发,软件工程,嵌入式,UI设计,重构,架构等IT企业培训</title>
<metaname="keywords"content="企业培训,企业内训,android培训,软考培训,it企业培训"/>
<metaname="description"content="安博中程企业培训多年来致力于android培训,软考培训,嵌入式,UI设计,重构,架构,软件测试,需求分析,软件开发,数据库,国防军工等IT高端技术的企业培训、企业内训,一直在行业内处于领先地位,所开企业内训或公开课如软考培训,android应用开发培训,软件工程培训,敏捷开发,嵌入式培训,项目管理及国防军工的电子产品设计和信息化培训等授课讲师均是知名专家,行业内领军人物,已成为中国高端培训第一品牌."/>
<scripttype="text/javascript">
<!--
varIE=false,FF=false,W=window,D=document,H,B,GET="getElementsByTagName",GEI="getElementById",qq=0;
functionfold(){
vare;
e=fixE(e);
if(e)element=fixElement(e);
element=element.parentNode.parentNode;
element.className=element.className.indexOf("hide")>0?"module":"modulehide";
}
varDrag={
draging:false,
x:0,
y:0,
element:null,
fDiv:null,
ghost:null,
addEvent:function(){vara=D[GET]("li");for(vari=a.length-1;i>-1;i--)if(a[i].className=="module")a[i].onmousedown=Drag.dragStart;},
ix:2,iy:7,
ox:6,oy:7,
fx:6,fy:6,
dragStart:function(e){
if(Drag.draging)return;
vare;
e=fixE(e);
if(e)element=fixElement(e);
D.getElementById("aaa").innerHTML=element.parentNode.offsetTop+","+element.parentNode.offsetHeight;
//测试
if(element.className!="title")return;
element=element.parentNode;
Drag.element=element;
//以上获得当前移动的模块
Drag.x=e.layerX?e.layerX+Drag.fx:(IE?e.x+Drag.ix:e.offsetX+Drag.ox);
Drag.y=e.layerY?e.layerY+Drag.fy:(IE?e.y+Drag.iy:e.offsetY+Drag.oy);
//鼠标相对于模块的位置
Drop.measure();
if(e.layerX){Drag.floatIt(e);Drag.drag(e);}//fixFF
B.style.cursor="move";
D.onmousemove=Drag.drag;
D.ondragstart=function(){window.event.returnValue=false;}
D.onselectstart=function(){window.event.returnValue=false;};
D.onselect=function(){returnfalse};
D.onmouseup=element.onmouseup=Drag.dragEnd;
element.onmousedown=null;
},
drag:function(e){
vare;
e=fixE(e);
if(!Drag.fDiv)Drag.floatIt(e);//forIE&Opera
varx=e.clientX,y=e.clientY;
Drag.fDiv.style.top=y+H.scrollTop-Drag.y+"px";
Drag.fDiv.style.left=x+H.scrollLeft-Drag.x+"px";
Drop.drop(x,y);
//statu(e);
},
dragEnd:function(e){
B.style.cursor="";
D.ondragstart=D.onmousemove=D.onselectstart=D.onselect=D.onmouseup=null;
Drag.element.onmousedown=Drag.dragStart;
if(!Drag.draging)return;
Drag.ghost.parentNode.insertBefore(Drag.element,Drag.ghost);
Drag.ghost.parentNode.removeChild(Drag.ghost);
B.removeChild(Drag.fDiv);
Drag.fDiv=null;
Drag.draging=false;
Drop.init(D[GEI]("container"));
},
floatIt:function(e){
vare,element=Drag.element;
varghost=D.createElement("LI");
Drag.ghost=ghost;
ghost.classname="moduleghost";
ghost.style.height=element.offsetHeight-2+"px";
element.parentNode.insertBefore(ghost,element);
//创建模块占位框
varfDiv=D.createElement("UL");
Drag.fDiv=fDiv;
fDiv.classname="float";
B.appendChild(fDiv);
fDiv.style.width=ghost.parentNode.offsetWidth+"px";
fDiv.appendChild(element);
//创建容纳模块的浮动层
Drag.draging=true;
}
}
varDrop={
root:null,
index:null,
column:null,
init:function(it){
if(!it)return;
Drop.root=it;
it.firstItem=it.lastItem=null;
vara=it[GET]("ul");
for(vari=0;i<a.length;i++){
if(a[i].className!="column")continue;
if(it.firstItem==null){
it.firstItem=a[i];
a[i].previousItem=null;
}else{
a[i].previousItem=a[i-1];
a[i-1].nextItem=a[i];
}
a[i].nextItem=null;
it.lastItem=a[i];
a[i].index=i;
a[i].firstItem=a[i].lastItem=null;
varb=a[i][GET]("li");
for(varj=0;j<b.length;j++){
if(b[j].className.indexOf("module")==-1)continue;
if(a[i].firstItem==null){
a[i].firstItem=b[j];
b[j].previousItem=null;
}else{
b[j].previousItem=b[j-1];
b[j-1].nextItem=b[j];
}
b[j].nextItem=null;
a[i].lastItem=b[j];
b[j].index=i+","+j;
}
}
},
measure:function(){
if(!Drop.root)return;
varcurrentColumn=Drop.root.firstItem;
while(currentColumn){
varcurrentModule=currentColumn.firstItem;
while(currentModule){
currentModule.minY=currentModule.offsetTop;
currentModule.maxY=currentModule.minY+currentModule.offsetHeight;
currentModule=currentModule.nextItem;
}
currentColumn.minX=currentColumn.offsetLeft;
currentColumn.maxX=currentColumn.minX+currentColumn.offsetWidth;
currentColumn=currentColumn.nextItem;
}
Drop.index=Drag.element.index;
},
drop:function(x,y){
if(!Drop.root)return;
varx,y,currentColumn=Drop.root.firstItem;
while(x>currentColumn.maxX)if(currentColumn.nextItem)currentColumn=currentColumn.nextItem;elsebreak;
varcurrentModule=currentColumn.lastItem;
if(currentModule)while(y<currentModule.maxY){
if(y>currentModule.minY-12){
if(Drop.index==currentModule.index)return;
Drop.index=currentModule.index;
if(currentModule.index==Drag.element.index){if(currentModule.nextItem)currentModule=currentModule.nextItem;elsebreak;}
currentColumn.insertBefore(Drag.ghost,currentModule);
Drop.column=null;
window.status=qq++;
return;
}elseif(currentModule.previousItem)currentModule=currentModule.previousItem;elsereturn;
}
if(Drop.column==currentColumn.index)return;
currentColumn.appendChild(Drag.ghost);
Drop.index=0;
Drop.column=currentColumn.index;
window.status=qq++;
}
}
varwebNote={
obj:null,
canEdit:function(e){
vare,element;
e=fixE(e);
element=fixElement(e);
if(element.className!='webNote')return;
if(typeofelement.contentEditable!="undefined"){
element.contentEditable=true;
element.style.borderColor='red';
element.focus();
webNote.obj=element;
}
},
cannotEdit:function(){
if(!webNote.obj)return;
if(typeofwebNote.obj.contentEditable!="undefined"){
webNote.obj.style.borderColor='#ffffe0';
webNote.obj.contentEditable=false;
webNote.obj=null;
}
}
}
functionfixE(e){vare;e=e?e:(window.event?window.event:null);returne}
functionfixElement(e){vare;returne.target?(e.target.nodeType==3?e.target.parentNode:e.target):e.srcElement;}
onload=function(){
B=D[GET]("body")[0];
H=D[GET]("html")[0];
Drop.init(D[GEI]("container"));
Drag.addEvent();
}
functionstatu(e){
vare,element;
element=fixElement(e);
varaa=D.getElementById("aaa");
aa.innerHTML="e.xy:("+e.x+","+e.y+")<br/>e.offsetXY:("+e.offsetX+","+e.offsetY+")<br/>e.clientXY:("+e.clientX+","+e.clientY+")<br/>element.offsetLeftTop:("+element.offsetLeft+","+element.offsetTop+")<br/>e.layerXY:("+e.layerX+","+e.layerY+")";
}
//-->
</script>
<styletype="text/css">
body,table,td,th,input,textarea,button,select{font:13px/16pxVerdana,"宋体",sans-serif;}
table{border-collapse:collapse;}
p{margin:0px;}
.container{margin:8px;}
.column{width:33%;margin:0px;padding:0px;float:left;overflow:hidden;}
.module{margin:5px;padding:0px;border:1pxsolid#dcd;position:relative;background-color:white;list-style:none;}
.title{background-color:#edf;padding:1px0px;width:100%;overflow:hidden;cursor:move;z-index:0;font-weight:bold;}
.cont{padding:3px;overflow:hidden;}
.hide.cont{display:none;}
.pageTitle{font-weight:bold;text-align:center;}
input.pageTitle{display:none;border:0px;padding:0px;width:100%;}
.webNote{background-color:#ffffe0;border:1pxsolid#ffffe0;}
textarea.webNote{display:none;overflow:hidden;padding:0px;border:0px;padding:0px;}
.ghost{border:1pxdashedred;}
.float{position:absolute;z-index:100;margin:0px;padding:0px;overflow:hidden;list-style:none;-moz-opacity:.75;filter:Alpha(opacity=75);}
</style>
</head>
<body>
<center><spanclass="pageTitle"onClick="with(this.nextSibling){value=this.innerHTML;style.display='block';focus()};this.style.display='none';">Youcaninputatopichere!</span><inputclass="pageTitle"onBlur="with(this.previousSibling){D.title=innerHTML=this.value?this.value:'Demo';style.display='block';};this.style.display='none'"/></center>
<divclass="container"id="container">
<ulclass="column">
<liclass="module">
<divclass="title"><spanonClick="fold()">+</span> Coodinate</div>
<divclass="cont"id="aaa">content</div>
</li>
<liclass="module">
<divclass="title"><spanonClick="fold()">+</span> contentEditable</div>
<divclass="cont"onClick="alert('this.contentEditable:'+this.contentEditable)">clickhere</div>
</li>
<liclass="module">
<divclass="title"><spanonClick="fold()">+</span> longlongago</div>
<divclass="cont">安博中程企业培训多年来致力于android培训,软考培训,嵌入式,UI设计,重构,架构,软件测试,需求分析,软件开发,数据库,国防军工等IT高端技术的企业培训、企业内训,一直在行业内处于领先地位,所开企业内训或公开课如软考培训,android应用开发培训,软件工程培训,敏捷开发,嵌入式培训,项目管理及国防军工的电子产品设计和信息化培训等授课讲师均是知名专家,行业内领军人物,已成为中国高端培训第一品牌.</div>
</li>
</ul>
<ulclass="column">
<liclass="module">
<divclass="title"><spanonClick="fold()">+</span> this.parentNode</div>
<divclass="cont"><buttononClick="alert(this.parentNode.parentNode.parentNode.innerHTML)">parentNode</button></div>
</li>
<liclass="module">
<divclass="title"><spanonClick="fold()">+</span> webNote</div>
<divclass="cont"><divclass="webNote"onblur="webNote.cannotEdit()"onClick="webNote.canEdit(event)">Youcannotesomethinghere.<br/><ahref="http://www.miiceic.org.cn/">中程在线</a><br/><ahref="http://www.miiceic.org.cn/">企业培训</a><br/><ahref="http://www.amboedu.com/">职业教育</a><br/><ahref="http://www.ciitn.com/">信息产业培训网</a></div><textareaclass="webNote"></textarea></div>
</li>
<liclass="module">
<divclass="title"><spanonClick="fold()">+</span> Google</div>
</li>
</ul>
<ulclass="column">
<liclass="module">
<divclass="title"><spanonClick="fold()">+</span> pia~~</div>
<divclass="cont"><ahref="http://www.miiceic.org.cn/">中程在线</a><ahref="http://www.miiceic.org.cn/">企业培训</a><ahref="http://www.amboedu.com/">职业教育</a><ahref="http://www.ciitn.com/">信息产业培训网</a></div>
</li>
<liclass="module">
<divclass="title"><spanonClick="fold()">+</span> biu~~</div>
<divclass="cont"><ahref="http://www.miiceic.org.cn/">中程在线</a><br/><ahref="http://www.miiceic.org.cn/">企业培训</a><br/><ahref="http://www.amboedu.com/">职业教育</a><br/><ahref="http://www.ciitn.com/">信息产业培训网</a></div>
</li>
<liclass="module">
<divclass="title"><spanonClick="fold()">+</span> 无话可说</div>
<divclass="cont"id="bbb"><ahref="http://www.miiceic.org.cn/">中程在线</a><ahref="http://www.miiceic.org.cn/">企业培训</a><ahref="http://www.amboedu.com/">职业教育</a><ahref="http://www.ciitn.com/">信息产业培训网</a></div>
</li>
</ul>
</div>
</body>
</html>