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


Posted in Javascript onAugust 09, 2011
JScript 文件: 
//检测浏览器 MSIE Firefox 
var ie=false,moz=false; 
(function() 
{//check the browser 
var userAgent=navigator.userAgent; 
if(userAgent.indexOf("MSIE")!=-1) 
ie=true; 
else if(userAgent.indexOf("Firefox")!=-1) 
moz=true; 
})(); 
//通过ID获得对象 
function $E_ID(idString) 
{ 
return document.getElementById(idString); 
} 
//通过Name获得对象 
function $Es_Tag(tagName) 
{ 
return document.getElementsByTagName(tagName); 
} 
//获得对象绝对位置 obj.offsetparent 
function $GetInfo(o) 
{ 
var to=new Object(); 
to.left=to.right=to.top=to.bottom=0; 
var twidth=o.offsetWidth; 
var theight=o.offsetHeight; 
while(o) 
{ 
to.left+=o.offsetLeft; 
to.top+=o.offsetTop; 
o=o.offsetParent; 
} 
to.right=to.left+twidth; 
to.bottom=to.top+theight; 
return to; 
} 
//鼠标点击对象确发事件 
function $hitTest(obj,event) 
{ 
obj=$GetInfo(obj); 
var x=event.clientX; 
var y=event.clientY; 
if((x>=obj.left&&x<=obj.right)&&(y>=obj.top&&y<=obj.bottom)) 
 return true; 
else 
 return false; 
} 
function Drag(event) 
{ 
this.dragged=false; 
this.ao=null; 
this.tdiv=null; 
this.fixLeft=0; 
this.fixTop=0; 
this.lastX=event.clientX; 
this.lastY=event.clientY; 
Drag.mm=null; 
this.dragStart=function(event) 
{ 
this.ao=ie?event.srcElement:(moz?event.target:null); 
if(ie) 
document.body.onselectstart=function() 
{ 
return false 

} 
if(moz&&this.ao.nodeType==3) 
 this.ao=this.ao.parentNode; 
if(this.ao.tagName=="TD"||this.ao.tagName=="TR") 
 this.ao=this.ao.offsetParent.parentNode; 
else 
 return; 
if(this.ao.className!="dragdiv") 
 return; 
this.tdiv=$E_ID("tmpdiv"); 
this.tdiv.style.visibility="visible"; 
this.tdiv.style.filter="alpha(opacity=70)"; 
if(ie) 
 this.tdiv.filters.alpha.opacity=70; 
this.tdiv.style.opacity=0.7; 
this.tdiv.style.zIndex=100; 
this.tdiv.innerHTML=this.ao.innerHTML; 
this.tdiv.style.width=this.ao.offsetWidth+"px"; 
this.tdiv.style.height=this.ao.offsetHeight+"px"; 
this.tdiv.style.top=$GetInfo(this.ao).top+"px"; 
this.tdiv.style.left=$GetInfo(this.ao).left+"px"; 
this.fixTop=parseInt($GetInfo(this.tdiv).top); 
this.fixLeft=parseInt($GetInfo(this.tdiv).left); 
this.dragged=true; 
} 
this.onDrag=function(event) 
{ 
if((!this.dragged)||this.ao==null)return; 
var tX=event.clientX; 
var tY=event.clientY; 
this.tdiv.style.left=parseInt(this.fixLeft+tX-this.lastX-document.body.scrollLeft)+"px"; 
this.tdiv.style.top=parseInt(this.fixTop+tY-this.lastY-document.body.scrollTop)+"px"; 
for(var m=0;m<$E_ID("root").rows.length;m++) 
{ 
var rootCells=$E_ID("root").rows[m].cells; 
for(var i=0;i<rootCells.length;i++) 
{ 
if($hitTest(rootCells[i],event)) 
{ 
if(rootCells[i].hasChildNodes()) 
{ 
for(var j=0;j<rootCells[i].childNodes.length;j++) 
{ 
if($hitTest(rootCells[i].childNodes[j],event)) 
{ 
rootCells[i].insertBefore(this.ao,rootCells[i].childNodes[j]); 
break; 
} 
} 
if(j==rootCells[i].childNodes.length) 
{ 
rootCells[i].appendChild(this.ao);break; 
} 
break; 
} 
else 
{ 
rootCells[i].appendChild(this.ao); 
break; 
} 
} 
} 
} 
} 
this.dragEnd=function() 
{ 
if(this.dragged) 
{ 
this.dragged=false; 
Drag.mm=this.repos(150,15,this); 
this.ao=null; 
} 
if(ie)document.body.onselectstart=function(){return true} 
} 
this.repos=function(aa,ab,obj) 
{ 
if(ie)var f=obj.tdiv.filters.alpha.opacity; 
else if(moz)var f=obj.tdiv.style.opacity*100; 
var kf=f/ab; 
var tl=parseInt($GetInfo(obj.tdiv).left); 
var tt=parseInt($GetInfo(obj.tdiv).top); 
var kl=(tl-$GetInfo(obj.ao).left)/ab; 
var kt=(tt-$GetInfo(obj.ao).top)/ab; 
return setInterval(function(){ 
if(ab<1) 
{ 
clearInterval(Drag.mm); 
obj.tdiv.style.visibility="hidden"; 
obj.tdiv.style.zIndex=""; 
return; 
} 
ab--; 
tl-=kl; 
tt-=kt; 
f-=kf; 
obj.tdiv.style.left=parseInt(tl)+"px"; 
obj.tdiv.style.top=parseInt(tt)+"px"; 
if(ie)obj.tdiv.filters.alpha.opacity=f; 
else if(moz)obj.tdiv.style.opacity=f/100; 
},aa/ab ); 
} 
} 
var tDrag=null; 
function init(event) 
{ 
// alert(event.target.innerHTML); 
tDrag=new Drag(event); 
tDrag.dragStart(event); 
} 
function move(event) 
{ 
if(tDrag!=null)tDrag.onDrag(event); 
} 
function end() 
{ 
if(tDrag!=null){ 
tDrag.dragEnd(); 
tDrag=null; 
} 
} 
Asp.net文件: 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" > 
<head > 
<title>Div拖动</title> 
<style type="text/css"> 
<!-- 
.dragHeader 
{ 
background-color:#e5eef9; 
border-top:1px solid #0066FF; 
height: 20px; 
cursor: move; 
font-weight: bold; 
} 
body 
{ 
font-family: Arial, Helvetica, sans-serif; 
font-size: 12px; 
} 
#root td 
{ 
vertical-align:top; 
border:1px dotted #666666; 
} 
#tmpdiv 
{ 
position: absolute; 
} 
.dragdiv 
{ 
} 
.style1 
{ 
color: #FFFFFF; 
font-weight: bold; 
font-size: 36px; 
} 
--> 
</style> 
<script type="text/javascript" src="DivDrag.js"></script> 
</head> 
<body onMouseDown="init(event)" onMouseUp="end()" onMouseMove="move(event)" > 
<script language="javascript" type="text/javascript" > 
document.write("<div id='tmpdiv'><\/div>"); 
</script> 
<table id="root" style="width:600px;height:300px" cellpadding="0" cellspacing="1" > 
<tr style="height:150px;width:600px"> 
<td style="width:200px; height: 50px;"> 
<div class="dragdiv" id="div1" > 
<table style="height:100%;width:100%" border ="0"> 
<tr> 
<td> 
<input id="Button1" type="button" value="button" /> 
可移动DIV1<br> 
点击即可开始拖动! 
</td> 
</tr> 
</table> 
</div> 
</td> 
<td style="width:200px; height: 50px;"> 
<div class="dragdiv" id="div2" 
<table style="height:100%;width:100%" border ="0"> 
<tr> 
<td> 
<input id="Button2" type="button" value="button" /> 
可移动DIV2<br> 
点击即可开始拖动! 
</td> 
</tr> 
</table> 
</div> 
</td> 
<td style="width:200px; height: 50px;"> 
<div class="dragdiv" id="div3" 
<table style="height:100%;width:100%" border ="0"> 
<tr> 
<td> 
<input id="Button3" type="button" value="button" /> 
可移动DIV3<br> 
点击即可开始拖动! 
</td> 
</tr> 
</table> 
</div> 
</td> 
</tr> 
<tr style="height:150px;width:600px"> 
<td style="width:200px; height: 50px;"> 
</td> 
<td style="width:200px; height: 50px;"> 
</td> 
<td style="width:200px; height: 50px;"> 
</td> 
</tr> 
<tr style="height:150px;width:600px"> 
<td style="width:200px; height: 50px;"> 
</td> 
<td style="width:200px; height: 50px;"> 
</td> 
<td style="width:200px; height: 50px;"> 
</td> 
</tr> 
</table> 
</body> 
</html>
Javascript 相关文章推荐
小米公司JavaScript面试题
Dec 29 Javascript
jquery获取checkbox的值并post提交
Jan 14 Javascript
js中日期的加减法
May 06 Javascript
Javascript节点关系实例分析
May 15 Javascript
jquery获取多个checkbox的值异步提交给php
Jul 07 Javascript
js中json处理总结之JSON.parse
Oct 14 Javascript
ionic进入多级目录后隐藏底部导航栏(tabs)的完美解决方案
Nov 23 Javascript
如何选择jQuery版本 1.x? 2.x? 3.x?
Apr 01 jQuery
详解React Native网络请求fetch简单封装
Aug 10 Javascript
如何理解Vue的.sync修饰符的使用
Aug 17 Javascript
详解如何在Vue里建立长按指令
Aug 20 Javascript
手写实现JS中的new
Nov 07 Javascript
基于Jquery的文字自动截取(提供源代码)
Aug 09 #Javascript
JQuery动态创建DOM、表单元素的实现代码
Aug 09 #Javascript
用JS判断IE版本的代码 超管用!
Aug 09 #Javascript
使用jQuery+HttpHandler+xml模拟一个三级联动的例子
Aug 09 #Javascript
js 分页全选或反选标识实现代码
Aug 09 #Javascript
js字符串的各种格式的转换 ToString,Format
Aug 08 #Javascript
Jquery ajax传递复杂参数给WebService的实现代码
Aug 08 #Javascript
You might like
Php部分常见问题总结
2006/10/09 PHP
PHP 防注入函数(格式化数据)
2011/08/08 PHP
php sybase_fetch_array使用方法
2014/04/15 PHP
php在linux下检测mysql同步状态的方法
2015/01/15 PHP
php二维码生成
2015/10/19 PHP
JQuery从头学起第二讲
2010/07/04 Javascript
JavaScript实现简单的tab选项卡切换
2016/01/05 Javascript
jQuery实现本地预览上传图片功能
2016/01/08 Javascript
jQuery简单实现上下,左右滑动的方法
2016/06/01 Javascript
JavaScript必知必会(三) String .的方法来自何方
2016/06/08 Javascript
又一款js时钟!transform实现时钟效果
2016/08/15 Javascript
基于Three.js插件制作360度全景图
2016/11/29 Javascript
浅谈html转义及防止javascript注入攻击的方法
2016/12/04 Javascript
vue-cli webpack 开发环境跨域详解
2017/05/18 Javascript
js 简易版滚动条实例(适用于移动端H5开发)
2017/06/26 Javascript
vue-cli中打包图片路径错误的解决方法
2017/10/26 Javascript
详解React native fetch遇到的坑
2018/08/30 Javascript
在 Vue 应用中使用 Netlify 表单功能的方法详解
2019/06/03 Javascript
在node环境下parse Smarty模板的使用示例代码
2019/11/15 Javascript
js实现登录时记住密码的方法分析
2020/04/05 Javascript
[01:04:48]VGJ.S vs TNC Supermajor 败者组 BO3 第一场 6.6
2018/06/07 DOTA
python操作MySQL数据库的方法分享
2012/05/29 Python
python自动化测试之连接几组测试包实例
2014/09/28 Python
BP神经网络原理及Python实现代码
2018/12/18 Python
详解python--模拟轮盘抽奖游戏
2019/04/12 Python
python利用JMeter测试Tornado的多线程
2020/01/12 Python
Python打包工具PyInstaller的安装与pycharm配置支持PyInstaller详细方法
2020/02/27 Python
python如何解析复杂sql,实现数据库和表的提取的实例剖析
2020/05/15 Python
解决pytorch 交叉熵损失输出为负数的问题
2020/07/07 Python
Python绘图之柱形图绘制详解
2020/07/28 Python
什么叫应用程序域?什么是托管代码?什么是强类型系统?什么是装箱和拆箱?什么是重载?CTS、CLS和CLR分别作何解释?
2012/05/23 面试题
3的组成教学反思
2014/04/30 职场文书
小学数学教研活动总结
2014/07/01 职场文书
高中学校对照检查材料
2014/08/31 职场文书
python的列表生成式,生成器和generator对象你了解吗
2022/03/16 Python
python 判断文件或文件夹是否存在
2022/03/18 Python