文章来源: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>学习中,分享一下,大家多给点意见啊~~~

解决方案 »

  1.   


    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中拖动更简单
      

  2.   

    发现一个BUG,在火狐里拖动久了,会卡住,无法在各层间切换,6永远只能在最上面!
      

  3.   

    是不是用jquery来做更方便啊,一句代码就搞定了
      

  4.   

    我晕死,真的有BUG,难道你们没有在FF下测试过吗??
      

  5.   

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