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 相关文章推荐
用js实现层随着内容大小动态渐变改变 推荐
Dec 19 Javascript
Js event事件在IE、FF兼容性问题
Jan 01 Javascript
JavaScript中将一个值转换为字符串的方法分析[译]
Sep 21 Javascript
jquery的ajax()函数传值中文乱码解决方法介绍
Nov 08 Javascript
jquery实现select下拉框美化特效代码分享
Aug 18 Javascript
直接拿来用的页面跳转进度条JS实现
Jan 06 Javascript
深入浅析JavaScript中with语句的理解
May 12 Javascript
详解Vue监听数据变化原理
Mar 08 Javascript
Angular 2父子组件之间共享服务通信的实现
Jul 04 Javascript
JavaScript键盘事件响应顺序详解
Sep 30 Javascript
Vue页面刷新记住页面状态的实现
Dec 27 Javascript
最全vue的vue-amap使用高德地图插件画多边形范围的示例代码
Jul 17 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
CPU步进是什么意思?i3-9100F B0步进和U0步进区别知识科普
2020/03/17 数码科技
PHP FOR MYSQL 代码生成助手(根据Mysql里的字段自动生成类文件的)
2011/07/23 PHP
优化PHP程序的方法小结
2012/02/23 PHP
利用谷歌 Translate API制作自己的翻译脚本
2014/06/04 PHP
php操作XML、读取数据和写入数据的实现代码
2014/08/15 PHP
php绘图之加载外部图片的方法
2015/01/24 PHP
JavaScript高级程序设计 读书笔记之九 本地对象Array
2012/02/27 Javascript
JavaScript格式化日期时间的方法和自定义格式化函数示例
2014/04/04 Javascript
javascript调试之DOM断点调试法使用技巧分享
2014/04/15 Javascript
jQuery获取选中内容及设置元素属性的方法
2014/07/09 Javascript
JSON相关知识汇总
2015/07/03 Javascript
Three.js的使用及绘制基础3D图形详解
2017/04/27 Javascript
纯JS实现只能输入数字的简单代码
2017/06/21 Javascript
echarts鼠标覆盖高亮显示节点及关系名称详解
2018/03/17 Javascript
vue element项目引入icon图标的方法
2018/06/06 Javascript
JavaScript中this用法学习笔记
2019/03/17 Javascript
nodejs搭建本地服务器并访问文件操作示例
2019/05/11 NodeJs
详解jenkins自动化部署vue
2019/05/14 Javascript
vue 组件中使用 transition 和 transition-group实现过渡动画
2019/07/09 Javascript
解决layer 动态加载select 失效的问题
2019/09/18 Javascript
[51:17]Mski vs VGJ.S Supermajor小组赛C组 BO3 第三场 6.3
2018/06/04 DOTA
python函数装饰器用法实例详解
2015/06/04 Python
Python使用迭代器打印螺旋矩阵的思路及代码示例
2016/07/02 Python
Python自动化运维之Ansible定义主机与组规则操作详解
2019/06/13 Python
python 公共方法汇总解析
2019/09/16 Python
使用python自动追踪你的快递(物流推送邮箱)
2020/03/17 Python
Keras预训练的ImageNet模型实现分类操作
2020/07/07 Python
html5启动原生APP总结
2020/07/03 HTML / CSS
Herve Leger官网:标志性绷带连衣裙等
2018/12/26 全球购物
银行会计财务工作个人的自我评价
2013/10/29 职场文书
班组长安全生产职责
2013/12/16 职场文书
中等生评语大全
2014/05/04 职场文书
2014教师党员自我评议(5篇)
2014/09/20 职场文书
公司聚餐通知
2015/04/22 职场文书
教你如何使用Python下载B站视频的详细教程
2021/04/29 Python
python中Pyqt5使用Qlabel标签播放视频
2022/04/22 Python