100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > Javascript实现的类似Google的Div拖动效果代码【javascript】

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

时间:2021-07-13 11:56:24

相关推荐

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

web前端|js教程

Google,Div拖动

web前端-js教程

手机看html源码,ubuntu 多屏切换,tomcat报线程暂挂,爬虫 自动答题,php添加用户怎么实现,做seo技术专注乐云seolzw

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.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;

}

}

文件:

Div拖动

<!--

.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;

}

-->

document.write("

");

简洁留言板源码,ubuntu下看代码,墨绿色爬虫,php私人,网站seo总监lzw

jsp 视频网站源码下载,ubuntu开权限代码,tomcat加载j不出页面,书评爬虫结果,php 接口格式,免费的关键词优化软件seo顾问lzw

<div

<div

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。