网管联盟 | 网管论坛 | 网管u家 | 网管博客 | 网管软件 | 网管求职 | 小游戏 | 网管搜索 | 网管原创 | 网管聚合 | 网管读摘 | 网管焦点 | 世界素材 | 会员投稿 | 会员中心 
中国网管联盟
Windows Linux Cisco 网络技术 数据库 黑客攻防 DotNet Java PHP 认证 新闻资讯 服务器 存储资讯 网络设备 网管学堂 技术专题 焦点 网吧频道
 当前位置: > bitsCN.com > DotNet > XML&Web > 页面元素可拖拽放置的实现原理  

页面元素可拖拽放置的实现原理

2007-05-29  作者:bitsCN整理  来源:中国网管联盟  点评 投稿 收藏

  

    基本上msn、microsoft Live 和GOOGLE个性主页 元素可拖动并放置的的实现原理就是对DOM的操作再加上JAVASCRIPT拖动元素。实现起来实际上并不难,但是要做到很美观并符合用户习惯就需要一些时间思考了。

网管u家u.bitscn@com

以下代码实现了上述功能,虽然很简单,但是基本上说明了原理。实现更高级的功能当然还需要改进。

网管u家u.bitsCN.com


 <!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; 网管u家www.bitscn.net
  border-bottom: lightgrey thin solid;
  background-color: dimgray;
 }
  </style>
 </head>
 <body>
  <div id="parentDiv" class="parentCss" style="width:100">
   <div class="itemCss" onmouseover="InsertDiv(this)">one 1</div>
   <div class="itemCss" onmouseover="InsertDiv(this)">two 2</div>
   <div class="itemCss" onmouseover="InsertDiv(this)">three 3</div>
   <div class="itemCss" onmouseover="InsertDiv(this)">four 4</div>
   <div class="itemCss" onmouseover="InsertDiv(this)">five 5</div>
   <div class="itemCss" onmouseover="InsertDiv(this)">six 6</div>
   <div class="itemCss" onmouseover="InsertDiv(this)">seven 7</div>
   <div class="itemCss" onmouseover="InsertDiv(this)">eight 8</div> 网管下载dl.bitscn.com
   <div class="itemCss" onmouseover="InsertDiv(this)">nine 9</div>
   <div class="itemCss" onmouseover="InsertDiv(this)">ten 10</div>
  </div>
  <script language="javascript">
<!--

 var obj,obj2;  //引发事件对象
 var rootNode;     //控制对象根节点

网管u家u.bitscn@com

 var IsDrag=false;   //是否抓起
 
 var NullDiv;  //空临时层
 
 var x,y;   //鼠标与控件的相对坐标
 
 window.onload = Prepare; //窗体加载时委托到Prepare
  
 function Prepare()
 {
  //生成临时层,并设置其属性
  NullDiv = document.createElement("div");
     
  //获得控制对象的根节点元素
  rootNode = document.getElementById("parentDiv");
  
  document.onmousemove=MoveIt; //当鼠标在文档上移动时事件委托到MoveIt
  
  document.onmousedown=Drag;   //当鼠标按下时事件委托到Drag
  
  document.onmouseup=Release;  //当鼠标释放台起时事件委托到Release
  
  
 } 
  
 function Drag()
 {
 
  obj = event.srcElement;
  
  x=0;//event.offsetX;

网管网www_bitscn_com

  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='';
网管u家u.bitscn@com

   NullDiv.style.height='18';
   NullDiv.style.width='100';
   rootNode.insertBefore(NullDiv,obj2);
  }
 }
 
//-->
  </script>
 </body>
</html>
网管联盟bitsCN_com

中国网管论坛bbs.bitsCN.com

TAGs   原理   实现   放置   元素   页面       <   >   div   //   /div>      
 上一篇:WebService中使用自定义类的解决方法   下一篇:XML文件读取数据绑定到DropDownList
页面元素可拖拽放置的实现原理 评论:
loading.. 评论加载中…
评论:请自觉遵守互联网相关政策法规,评论不得超过250字。

验证码: 注册用户
本类热门排行:
最新推荐文章:
网管论坛交流: