Discuz教程网

Javascript实现的类似Google的Div拖动效果代码

[复制链接]
authicon dly 发表于 2011-9-12 15:13:08 | 显示全部楼层 |阅读模式
  1. JScript 文件:
  2. //检测浏览器 MSIE Firefox
  3. var ie=false,moz=false;
  4. (function()
  5. {//check the browser
  6. var userAgent=navigator.userAgent;
  7. if(userAgent.indexOf("MSIE")!=-1)
  8. ie=true;
  9. else if(userAgent.indexOf("Firefox")!=-1)
  10. moz=true;
  11. })();
  12. //通过ID获得对象
  13. function $E_ID(idString)
  14. {
  15. return document.getElementById(idString);
  16. }
  17. //通过Name获得对象
  18. function $Es_Tag(tagName)
  19. {
  20. return document.getElementsByTagName(tagName);
  21. }
  22. //获得对象绝对位置 obj.offsetparent
  23. function $GetInfo(o)
  24. {
  25. var to=new Object();
  26. to.left=to.right=to.top=to.bottom=0;
  27. var twidth=o.offsetWidth;
  28. var theight=o.offsetHeight;
  29. while(o)
  30. {
  31. to.left+=o.offsetLeft;
  32. to.top+=o.offsetTop;
  33. o=o.offsetParent;
  34. }
  35. to.right=to.left+twidth;
  36. to.bottom=to.top+theight;
  37. return to;
  38. }
  39. //鼠标点击对象确发事件
  40. function $hitTest(obj,event)
  41. {
  42. obj=$GetInfo(obj);
  43. var x=event.clientX;
  44. var y=event.clientY;
  45. if((x>=obj.left&&x<=obj.right)&&(y>=obj.top&&y<=obj.bottom))
  46.  return true;
  47. else
  48.  return false;
  49. }
  50. function Drag(event)
  51. {
  52. this.dragged=false;
  53. this.ao=null;
  54. this.tdiv=null;
  55. this.fixLeft=0;
  56. this.fixTop=0;
  57. this.lastX=event.clientX;
  58. this.lastY=event.clientY;
  59. Drag.mm=null;
  60. this.dragStart=function(event)
  61. {
  62. this.ao=ie?event.srcElement:(moz?event.target:null);
  63. if(ie)
  64. document.body.onselectstart=function()
  65. {
  66.   return false
  67.   }
  68. if(moz&&this.ao.nodeType==3)
  69.  this.ao=this.ao.parentNode;
  70. if(this.ao.tagName=="TD"||this.ao.tagName=="TR")
  71.  this.ao=this.ao.offsetParent.parentNode;
  72. else
  73.  return;
  74. if(this.ao.className!="dragdiv")
  75.  return;
  76. this.tdiv=$E_ID("tmpdiv");
  77. this.tdiv.style.visibility="visible";
  78. this.tdiv.style.filter="alpha(opacity=70)";
  79. if(ie)
  80.  this.tdiv.filters.alpha.opacity=70;
  81. this.tdiv.style.opacity=0.7;
  82. this.tdiv.style.zIndex=100;
  83. this.tdiv.innerHTML=this.ao.innerHTML;
  84. this.tdiv.style.width=this.ao.offsetWidth+"px";
  85. this.tdiv.style.height=this.ao.offsetHeight+"px";
  86. this.tdiv.style.top=$GetInfo(this.ao).top+"px";
  87. this.tdiv.style.left=$GetInfo(this.ao).left+"px";
  88. this.fixTop=parseInt($GetInfo(this.tdiv).top);
  89. this.fixLeft=parseInt($GetInfo(this.tdiv).left);
  90. this.dragged=true;
  91. }
  92. this.onDrag=function(event)
  93. {
  94. if((!this.dragged)||this.ao==null)return;
  95. var tX=event.clientX;
  96. var tY=event.clientY;
  97. this.tdiv.style.left=parseInt(this.fixLeft+tX-this.lastX-document.body.scrollLeft)+"px";
  98. this.tdiv.style.top=parseInt(this.fixTop+tY-this.lastY-document.body.scrollTop)+"px";
  99. for(var m=0;m<$E_ID("root").rows.length;m++)
  100. {
  101. var rootCells=$E_ID("root").rows[m].cells;
  102. for(var i=0;i<rootCells.length;i++)
  103. {
  104. if($hitTest(rootCells[i],event))
  105. {
  106. if(rootCells[i].hasChildNodes())
  107. {
  108. for(var j=0;j<rootCells[i].childNodes.length;j++)
  109. {
  110. if($hitTest(rootCells[i].childNodes[j],event))
  111. {
  112. rootCells[i].insertBefore(this.ao,rootCells[i].childNodes[j]);
  113. break;
  114. }
  115. }
  116. if(j==rootCells[i].childNodes.length)
  117. {
  118. rootCells[i].appendChild(this.ao);break;
  119. }
  120. break;
  121. }
  122. else
  123. {
  124. rootCells[i].appendChild(this.ao);
  125. break;
  126. }
  127. }
  128. }
  129. }
  130. }
  131. this.dragEnd=function()
  132. {
  133. if(this.dragged)
  134. {
  135. this.dragged=false;
  136. Drag.mm=this.repos(150,15,this);
  137. this.ao=null;
  138. }
  139. if(ie)document.body.onselectstart=function(){return true}
  140. }
  141. this.repos=function(aa,ab,obj)
  142. {
  143. if(ie)var f=obj.tdiv.filters.alpha.opacity;
  144. else if(moz)var f=obj.tdiv.style.opacity*100;
  145. var kf=f/ab;
  146. var tl=parseInt($GetInfo(obj.tdiv).left);
  147. var tt=parseInt($GetInfo(obj.tdiv).top);
  148. var kl=(tl-$GetInfo(obj.ao).left)/ab;
  149. var kt=(tt-$GetInfo(obj.ao).top)/ab;
  150. return setInterval(function(){
  151. if(ab<1)
  152. {
  153. clearInterval(Drag.mm);
  154. obj.tdiv.style.visibility="hidden";
  155. obj.tdiv.style.zIndex="";
  156. return;
  157. }
  158. ab--;
  159. tl-=kl;
  160. tt-=kt;
  161. f-=kf;
  162. obj.tdiv.style.left=parseInt(tl)+"px";
  163. obj.tdiv.style.top=parseInt(tt)+"px";
  164. if(ie)obj.tdiv.filters.alpha.opacity=f;
  165. else if(moz)obj.tdiv.style.opacity=f/100;
  166. },aa/ab );
  167. }
  168. }
  169. var tDrag=null;
  170. function init(event)
  171. {
  172. // alert(event.target.innerHTML);
  173. tDrag=new Drag(event);
  174. tDrag.dragStart(event);
  175. }
  176. function move(event)
  177. {
  178. if(tDrag!=null)tDrag.onDrag(event);
  179. }
  180. function end()
  181. {
  182. if(tDrag!=null){
  183. tDrag.dragEnd();
  184. tDrag=null;
  185. }
  186. }
  187. Asp.net文件:
  188. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  189. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  190. <html xmlns="http://www.w3.org/1999/xhtml" >
  191. <head >
  192. <title>Div拖动</title>
  193. <style type="text/css">
  194. <!--
  195. .dragHeader
  196. {
  197. background-color:#e5eef9;
  198. border-top:1px solid #0066FF;
  199. height: 20px;
  200. cursor: move;
  201. font-weight: bold;
  202. }
  203. body
  204. {
  205. font-family: Arial, Helvetica, sans-serif;
  206. font-size: 12px;
  207. }
  208. #root td
  209. {
  210. vertical-align:top;
  211. border:1px dotted #666666;
  212. }
  213. #tmpdiv
  214. {
  215. position: absolute;
  216. }
  217. .dragdiv
  218. {
  219. }
  220. .style1
  221. {
  222. color: #FFFFFF;
  223. font-weight: bold;
  224. font-size: 36px;
  225. }
  226. -->
  227. </style>
  228. <script type="text/javascript" src="DivDrag.js"></script>
  229. </head>
  230. <body onMouseDown="init(event)" onMouseUp="end()" onMouseMove="move(event)" >
  231. <script language="javascript" type="text/javascript" >
  232. document.write("<div id='tmpdiv'><\/div>");
  233. </script>
  234. <table id="root" style="width:600px;height:300px" cellpadding="0" cellspacing="1" >
  235. <tr style="height:150px;width:600px">
  236. <td style="width:200px; height: 50px;">
  237. <div class="dragdiv" id="div1" >
  238. <table style="height:100%;width:100%" border ="0">
  239. <tr>
  240. <td>
  241. <input id="Button1" type="button" value="button" />
  242. 可移动DIV1<br>
  243. 点击即可开始拖动!
  244. </td>
  245. </tr>
  246. </table>
  247. </div>
  248. </td>
  249. <td style="width:200px; height: 50px;">
  250. <div class="dragdiv" id="div2"
  251. <table style="height:100%;width:100%" border ="0">
  252. <tr>
  253. <td>
  254. <input id="Button2" type="button" value="button" />
  255. 可移动DIV2<br>
  256. 点击即可开始拖动!
  257. </td>
  258. </tr>
  259. </table>
  260. </div>
  261. </td>
  262. <td style="width:200px; height: 50px;">
  263. <div class="dragdiv" id="div3"
  264. <table style="height:100%;width:100%" border ="0">
  265. <tr>
  266. <td>
  267. <input id="Button3" type="button" value="button" />
  268. 可移动DIV3<br>
  269. 点击即可开始拖动!
  270. </td>
  271. </tr>
  272. </table>
  273. </div>
  274. </td>
  275. </tr>
  276. <tr style="height:150px;width:600px">
  277. <td style="width:200px; height: 50px;">
  278. </td>
  279. <td style="width:200px; height: 50px;">
  280. </td>
  281. <td style="width:200px; height: 50px;">
  282. </td>
  283. </tr>
  284. <tr style="height:150px;width:600px">
  285. <td style="width:200px; height: 50px;">
  286. </td>
  287. <td style="width:200px; height: 50px;">
  288. </td>
  289. <td style="width:200px; height: 50px;">
  290. </td>
  291. </tr>
  292. </table>
  293. </body>
  294. </html>
复制代码




上一篇:取得窗口大小 兼容所有浏览器的js代码
下一篇:基于Jquery的文字自动截取(提供源代码)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 20:30

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表