js 实现div定位

<html>
<head>
<mce:style>
<!--
body {  
-moz-user-focus:  ignore;  
-moz-user-input:  disabled;  
-moz-user-select:  none;  
}    
.divall{
width:300px;
float:left;
border:1px solid #cccccc;
margin:10px 10px 0px 0px;
}
.divtitle{
height:20px;
border-bottom:1px solid #cccccc;
background-color:#eeeeee;
line-height: 20px;
}
.divtitle1{
float:left;
width:250px;
cursor: move;
}
.divtitle2{
float:right;
width:40px;
}
.divcontent{
height:170px;
width:300px;
}
--></mce:style><style mce_bogus="1">body {  
-moz-user-focus:  ignore;  
-moz-user-input:  disabled;  
-moz-user-select:  none;  
}    
.divall{
width:300px;
float:left;
border:1px solid #cccccc;
margin:10px 10px 0px 0px;
}
.divtitle{
height:20px;
border-bottom:1px solid #cccccc;
background-color:#eeeeee;
line-height: 20px;
}
.divtitle1{
float:left;
width:250px;
cursor: move;
}
.divtitle2{
float:right;
width:40px;
}
.divcontent{
height:170px;
width:300px;
}</style>
<mce:script type="text/javascript"><!--
function move(element, e) {
    var target = element;
    var parent = element.parentNode;
    var zIndex = element.style.zIndex;
    
    // 获取目标div的左上角坐标
    var left = target.offsetLeft;
    var top = target.offsetTop;
    while(target = target.offsetParent){
        left += target.offsetLeft;
        top += target.offsetTop;
    }
    
    // 复制一个div以占据目标div的起始位置
    var newObj = document.createElement("div");
    newObj.innerHTML = element.innerHTML;
    newObj.className = element.className;
    parent.insertBefore(newObj, element);
    // 将目标div改为绝对定位并赋上初使坐标值
    element.style.position = "absolute";
    element.style.left = left;
    element.style.top = top;
    element.style.zIndex = 1000;
    element.style.backgroundColor = "#dddddd";
    
    // 注册事件
    if (document.addEventListener) {
        document.addEventListener("mousemove", startMove, true);
        document.addEventListener("mouseup", stopMove, true);
    } else {
        document.onmousemove = startMove;
        document.onmouseup = stopMove;
    }
    
    e = e || event;
    var deltaX = e.clientX - parseInt(element.style.left);
    var deltaY = e.clientY - parseInt(element.style.top);
    
    // 开始移动
    function startMove(e) {
        e = e || event;
        element.style.left = e.clientX - deltaX + "px";
        element.style.top = e.clientY - deltaY + "px";
    };
    // 停止移动并重新定位
    function stopMove(e) {
        e = e || event;
        var mouseX = e.clientX;
        var mouseY = e.clientY;
        
        // 取消注册的事件
        if (document.removeEventListener) {
            document.removeEventListener("mousemove", startMove, true);
            document.removeEventListener("mouseup", stopMove, true);
        } else {
            document.onmousemove = "";
            document.onmouseup = "";
        }
        
        // 根据鼠标移动情况重新排序div对象
        var divs = parent.childNodes;
        for(var i = 0; i < divs.length; i++){
            if(divs[i].tagName != "DIV")
                continue;
                
            var obj = divs[i];
            var left = obj.offsetLeft;
            var top = obj.offsetTop;
            var width = obj.clientWidth;
            var height = obj.clientHeight;
            
            while(obj = obj.offsetParent){
                left += obj.offsetLeft;
                top += obj.offsetTop;
            }
            
            // 判断鼠标松开的时候是否在某个div对象的可见区域内,如果为true,则将目标div放置到该div对象的前面
            if(left <= mouseX && mouseX <= left+width && top <= mouseY && mouseY <= top+height){
                parent.insertBefore(element, divs[i]);
            }
        }
        
        // 将目标div改回相对定位
        element.style.position = "relative";
        element.style.left = "0px";
        element.style.top = "0px";
        element.style.zIndex = zIndex;
        element.style.backgroundColor = "";
        
        // 删除占位div对象
        parent.removeChild(newObj);
    };
    return true;
}
function openCloseDiv(element, id){
    var target = document.getElementById(id);
    if(target.style.display == 'none'){
        target.style.display = 'block';
        element.innerHTML = '收起';
    } else {
        target.style.display = 'none';
        element.innerHTML = '展开';
    }
}
// --></mce:script>
<title>相对定位div块移动_自动排列</title>
</head>
<body onselectstart='return false;'>
<div id="mainDiv" style="width:950px;">
    <div id="div1" class="divall">
        <div class="divtitle">
            <div class="divtitle1"  onMouseDown="return move(this.parentNode.parentNode,event);"><a href="javascript:void(0)" mce_href="javascript:void(0)">标题1</a></div>
            <div class="divtitle2">
                <div style="cursor: pointer;" mce_style="cursor: pointer;" οnclick="openCloseDiv(this, this.parentNode.parentNode.parentNode.id + 'content');">收起</div>
            </div>
        </div>
        <div id="div1content" class="divcontent">内容1</div>
    </div>
    
    <div id="div2" class="divall">
        <div class="divtitle">
            <div class="divtitle1"  onMouseDown="return move(this.parentNode.parentNode,event);"><a href="javascript:void(0)" mce_href="javascript:void(0)">标题2</a></div>
            <div class="divtitle2">
                <div style="cursor: pointer;" mce_style="cursor: pointer;" οnclick="openCloseDiv(this, this.parentNode.parentNode.parentNode.id + 'content');">收起</div>
            </div>
        </div>
        <div id="div2content" class="divcontent">内容2</div>
    </div>
    
    <div id="div3" class="divall">
        <div class="divtitle">
            <div class="divtitle1"  onMouseDown="return move(this.parentNode.parentNode,event);"><a href="javascript:void(0)" mce_href="javascript:void(0)">标题3</a></div>
            <div class="divtitle2">
                <div style="cursor: pointer;" mce_style="cursor: pointer;" οnclick="openCloseDiv(this, this.parentNode.parentNode.parentNode.id + 'content');">收起</div>
            </div>
        </div>
        <div id="div3content" class="divcontent">内容3</div>
    </div>
    
    <div id="div4" class="divall">
        <div class="divtitle">
            <div class="divtitle1"  onMouseDown="return move(this.parentNode.parentNode,event);"><a href="javascript:void(0)" mce_href="javascript:void(0)">标题4</a></div>
            <div class="divtitle2">
                <div style="cursor: pointer;" mce_style="cursor: pointer;" οnclick="openCloseDiv(this, this.parentNode.parentNode.parentNode.id + 'content');">收起</div>
            </div>
        </div>
        <div id="div4content" class="divcontent">内容4</div>
    </div>
    
    <div id="div5" class="divall">
        <div class="divtitle">
            <div class="divtitle1"  onMouseDown="return move(this.parentNode.parentNode,event);"><a href="javascript:void(0)" mce_href="javascript:void(0)">标题5</a></div>
            <div class="divtitle2">
                <div style="cursor: pointer;" mce_style="cursor: pointer;" οnclick="openCloseDiv(this, this.parentNode.parentNode.parentNode.id + 'content');">收起</div>
            </div>
        </div>
        <div id="div5content" class="divcontent">内容5</div>
    </div>
    
    <div id="div6" class="divall">
        <div class="divtitle">
            <div class="divtitle1"  onMouseDown="return move(this.parentNode.parentNode,event);"><a href="javascript:void(0)" mce_href="javascript:void(0)">标题6</a></div>
            <div class="divtitle2">
                <div style="cursor: pointer;" mce_style="cursor: pointer;" οnclick="openCloseDiv(this, this.parentNode.parentNode.parentNode.id + 'content');">收起</div>
            </div>
        </div>
        <div id="div6content" class="divcontent">内容6</div>
    </div>
    
    <div id="div7" class="divall">
        <div class="divtitle">
            <div class="divtitle1"  onMouseDown="return move(this.parentNode.parentNode,event);"><a href="javascript:void(0)" mce_href="javascript:void(0)">标题7</a></div>
            <div class="divtitle2">
                <div style="cursor: pointer;" mce_style="cursor: pointer;" οnclick="openCloseDiv(this, this.parentNode.parentNode.parentNode.id + 'content');">收起</div>
            </div>
        </div>
        <div id="div7content" class="divcontent">内容7</div>
    </div>
    
    <div id="div8" class="divall">
        <div class="divtitle">
            <div class="divtitle1"  onMouseDown="return move(this.parentNode.parentNode,event);"><a href="javascript:void(0)" mce_href="javascript:void(0)">标题8</a></div>
            <div class="divtitle2">
                <div style="cursor: pointer;" mce_style="cursor: pointer;" οnclick="openCloseDiv(this, this.parentNode.parentNode.parentNode.id + 'content');">收起</div>
            </div>
        </div>
        <div id="div8content" class="divcontent">内容8</div>
    </div>
    
    <div style="width:300px;height:190px;float:left;margin:10px 10px 0px 0px;"> </div>
    <div style="clear: both;" mce_style="clear: both;"></div>
    
</div>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值