MicroSoft Live 和GOOGLE個性主頁 頁面元素可拖拽放置的實現原理

<!doctype HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
 <head>
  <title>DropLayer2</title>
  <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
  <meta name="CODE_LANGUAGE" Content="C#">
  <meta name="vs_defaultClientScript" content="JavaScript">
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
  <style type="text/css">
    div
 {
  border-right: lightgrey thin solid;
  border-top: lightgrey thin solid;
  font-weight: bold;
  z-index: 2;
  text-transform: capitalize;
  border-left: lightgrey thin solid;
  color: white;
  border-bottom: lightgrey thin solid;
  background-color: dimgray;
 }
  </style>
 </head>
 <body>
  <div id="parentDiv" class="parentCss" style="width:100">
   <div class="itemCss" οnmοuseοver="InsertDiv(this)">one 1</div>
   <div class="itemCss" οnmοuseοver="InsertDiv(this)">two 2</div>
   <div class="itemCss" οnmοuseοver="InsertDiv(this)">three 3</div>
   <div class="itemCss" οnmοuseοver="InsertDiv(this)">four 4</div>
   <div class="itemCss" οnmοuseοver="InsertDiv(this)">five 5</div>
   <div class="itemCss" οnmοuseοver="InsertDiv(this)">six 6</div>
   <div class="itemCss" οnmοuseοver="InsertDiv(this)">seven 7</div>
   <div class="itemCss" οnmοuseοver="InsertDiv(this)">eight 8</div>
   <div class="itemCss" οnmοuseοver="InsertDiv(this)">nine 9</div>
   <div class="itemCss" οnmοuseοver="InsertDiv(this)">ten 10</div>
  </div>
  <script language="javascript">
<!--

 var obj,obj2;  //引發事件對象
 var rootNode;     //控制對象根節點

 var IsDrag=false;   //是否抓起
 
 var NullDiv;  //空臨時層
 
 var x,y;   //鼠標與控件的相對座標
 
 window.onload = Prepare; //窗體加載時委託到Prepare
  
 function Prepare()
 {
  //生成臨時層,並設置其屬性
  NullDiv = document.createElement("div");
     
  //獲得控制對象的根節點元素
  rootNode = document.getElementById("parentDiv");
  
  document.οnmοusemοve=MoveIt; //當鼠標在文檔上移動時事件委託到MoveIt
  
  document.οnmοusedοwn=Drag;   //當鼠標按下時事件委託到Drag
  
  document.οnmοuseup=Release;  //當鼠標釋放臺起時事件委託到Release
  
  
 } 
  
 function Drag()
 {
 
  obj = event.srcElement;
  
  x=0;//event.offsetX;
  y=0;//event.offsetY;
    
  obj.style.position='absolute';
  
  obj.style.posTop=event.y-y;
  obj.style.posLeft=event.x-x;
    
  IsDrag=true;
 }
 
 function MoveIt()
 {
  //window.status = event.x+"|"+event.y+"|"+IsDrag+"|"+x+"|"+y;
  
  if(IsDrag)
  {
   obj.style.posTop=event.y-y;
   obj.style.posLeft=event.x-x;
  }
 }
 
 function Release()
 {
  IsDrag=false;
  
  NullDiv.style.display='none';
  
  obj.style.position='';
  
  rootNode.insertBefore(obj,obj2);
 }
 
 function InsertDiv(o)
 {
  if(IsDrag)
  { 
   obj2=o;
   NullDiv.style.display='';
   NullDiv.style.height='18';
   NullDiv.style.width='100';
   rootNode.insertBefore(NullDiv,obj2);
  }
 }
 
//-->
  </script>
 </body>
</html>

 

<hr/>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章