可拖動的DIV

 
 
<script> 
 
var __DragId, __eDragSrc, __DragTop, __DragLeft; 
function __OnDrag(){ 
    var _element = event.srcElement; 
    if(_element.eDrag){ 
        if(event.type=="mousedown"){ 
            if(_element.eDragSrc){ 
                __eDragSrc = document.getElementById(_element.eDragSrc); 
                __eDragSrc.style.display = "block"; 
            } 
            document.body.style.cursor = "default"; 
            __DragId = document.getElementById(_element.eDrag); 
            __DragLeft = parseInt(__DragId.offsetWidth)-(parseInt(__DragId.offsetWidth)-(event.x-parseInt(__DragId.offsetLeft))); 
            __DragTop = parseInt(__DragId.offsetHeight)-(parseInt(__DragId.offsetHeight)-(event.y-parseInt(__DragId.offsetTop))); 
        } 
        if(event.type=="mouseup"){ 
            if(__DragId){ 
                __DragId = null; 
            } 
            if(__eDragSrc){ 
                __eDragSrc.style.display = "none"; 
                __eDragSrc = null; 
            } 
        } 
    } 
    if(event.type=="mousemove"){ 
            if(__DragId){ 
                __DragId.style.top = event.y - __DragTop; 
                __DragId.style.left = event.x - __DragLeft; 
                __DragId.style.margin = "auto"; 
            } 
    }  
} 
document.attachEvent("onmousedown", __OnDrag); 
document.attachEvent("onmousemove", __OnDrag); 
document.attachEvent("onmouseup", __OnDrag); 
</script> 
<table border="1" width="100%" id="d" style="font-size: 9pt; color: #FFFFFF; position: absolute"> 
     <tr> 
           <td bgcolor="#008000" eDrag="d">12</td> 
     </tr> 
     <tr> 
           <td> </td> 
     </tr> 
</table> 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章