目录
前言
本篇仅对拖拽过程中核心几个事件监听的写法进行小结,不涉及页面样式效果的展现。
实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
function dragDom(idName,toolName){ var toolNameVal = typeof toolName == "string" ? toolName : false; var oDiv=document.getElementById(idName); var currentTop,currentLeft,zIndexVal; if(toolName){ var targetOffsetLeft = $("#"+toolName).offset().left; var targetOffsetTop = $("#"+toolName).offset().top; } oDiv.onmousedown = function(ev){ var oEvent= ev || event; var disX = oEvent.clientX - oDiv.offsetLeft; var disY = oEvent.clientY - oDiv.offsetTop; zIndexVal = $(oDiv).css("z-index");
if(oDiv.setCapture){ oDiv.onmousemove=fnMove; oDiv.onmouseup=fnUp;
oDiv.setCapture(); } else{ document.onmousemove=fnMove; document.onmouseup=fnUp; }
function fnMove(ev){ var oEvent=ev||event; if(toolName=="key_index"){ currentLeft = oDiv.style.left = (oEvent.clientX-disX) <= (0-targetOffsetLeft) ? (0-targetOffsetLeft) + 'px' : (oEvent.clientX-disX) + 'px'; currentTop = oDiv.style.top = (oEvent.clientY-disY)<=(0-targetOffsetTop)? (0-targetOffsetTop)+'px' : (oEvent.clientY-disY)+'px'; } else { ... ... } oDiv.style.zIndex = 1200; }
function fnUp(){ this.onmousemove=null; this.onmouseup=null;
if(this.releaseCapture){ this.releaseCapture(); } if(toolName == "key_index"){ currentTop = parseInt(currentTop) + parseInt(document.body.scrollTop) - 120; currentLeft = parseInt(currentLeft) + 168; $(oDiv).parents("dl").removeAttr("style"); if(currentTop >= 0){ createKeyIndex(currentTop,currentLeft,"main_work_content","key_warp"); } currentTop = oDiv.style.left = 0; currentLeft = oDiv.style.top = 0; $(oDiv).removeAttr("style"); }else if(toolName == "time_trend"){ } }
}; }
|