js简单实现多个div拖动效果

kikiyaya 2010-07-15 10:26:30
文章来源:http://www.phphubei.com/thread-325-1-1.html

要在OA里做个便签模块,我想把它做成类似于许愿墙的效果,每条记录放在单独的层里,通过js让每个div可拖动
<!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>
<style type="text/css">
#main div{position:absolute;width:220px;height:150px;border:1px solid #999;}
</style>
<script type="text/javascript">
var a;
document.onmouseup=function(){
if(!a)return;
document.all?a.releaseCapture():window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
a="";
};
document.onmousemove=function (d){
if(!a) return;
if(!d) d=event;
a.style.left=(d.clientX-b)+"px";a.style.top=(d.clientY-c)+"px";
};
function move(o,e){
a=o;
document.all?a.setCapture():window.captureEvents(Event.MOUSEMOVE);
b=e.clientX-parseInt(a.style.left);
c=e.clientY-parseInt(a.style.top);
o.style.zIndex=getMaxIndex()+1;
}
function $(id){return document.getElementById(id);}
function getMaxIndex(){
var index=0;
var ds=$('main').getElementsByTagName('div');
var l=$('main').getElementsByTagName('div').length;

for (i=0;i<l;i++)
{
if (ds[i].style.zIndex>index) index=ds[i].style.zIndex;
}
return index;
}
</script>
</head>
<body>
<div id="main">
<div style="left:100px;top:100px;background:#fc9;" onmousedown="move(this,event)">1</div>
<div style="left:400px;top:100px;background:#9cf;" onmousedown="move(this,event)">2</div>
<div style="left:700px;top:100px;background:#f9c;" onmousedown="move(this,event)">3</div>
<div style="left:100px;top:300px;background:#9fc;" onmousedown="move(this,event)">4</div>
<div style="left:400px;top:300px;background:#c9f;" onmousedown="move(this,event)">5</div>
<div style="left:700px;top:300px;background:#cf9;" onmousedown="move(this,event)">6</div>
</div>
</body>
</html>

学习中,分享一下,大家多给点意见啊~~~
...全文
1061 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
luozhangwen 2010-08-24
  • 打赏
  • 举报
回复


<html>
<head>
<title></title>

<script language="javascript" type="text/javascript">
function div2() {
alert("div2");
/*
if (event.stopPropagation){
event.stopPropagation(); //兼容ie方法
} else{
event.cancelBubble = true; //兼容ie方法
}
*/
}
function div3() {
alert("div3");
event.cancelBubble = true;
}

function div1() {
alert("div1");
}
</script>

</head>
<body>
<form id="form1" runat="server">
<div id ="div1" onclick="div1()">
第一层DIV
<div id="div1" onclick="div2()"> <!--此处会变乱, 会调用2个div事件-->
第二层DIV(因为已经把代码注释了, 所以这里会变乱)
<!--加event.cancelBubble=true",只调用当前div事件,不调用外层DIV事件-->
<div onclick="div3()">第三层DIV </div>
</div>
</div>
</form>
</body>
</html>
ziyouren521125 2010-07-16
  • 打赏
  • 举报
回复
是不是用jquery来做更方便啊,一句代码就搞定了
kaifadi 2010-07-16
  • 打赏
  • 举报
回复
我晕死,真的有BUG,难道你们没有在FF下测试过吗??
kaifadi 2010-07-16
  • 打赏
  • 举报
回复
发现一个BUG,在火狐里拖动久了,会卡住,无法在各层间切换,6永远只能在最上面!
zoujp_xyz 2010-07-15
  • 打赏
  • 举报
回复
看看。。。。
luozhangwen 2010-07-15
  • 打赏
  • 举报
回复
wang15072732418 2010-07-15
  • 打赏
  • 举报
回复
向楼主学习
hoojo 2010-07-15
  • 打赏
  • 举报
回复

http://blog.csdn.net/IBM_hoojo/archive/2010/07/02/5708697.aspx
<script>
document.execCommand("2D-position",false,true);
</script>
<DIV contentEditable=true>
<DIV style="WIDTH: 300px; POSITION: absolute; HEIGHT: 100px; BACKGROUND-COLOR: red">移动层</DIV>
</DIV>
IE中拖动更简单

87,901

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧