/ Published in: JavaScript
License: public domain (retention of authorship message in code is appreciated). Size: 3.94 kB. To enable drag-drop capability on one or more elements, use enableDragDrop(elementIDArray, dropLocation);, where elementIDArray is an array of the IDs of elements to be given drag-drop capability, and dropLocation is an optional parameter which will return the dragged elements to their original position if they are dropped outside of the perimeter of the element with the ID dropLocation. To disable drag-drop capability on one or more elements, use disableDragDrop(elementIDArray);.
Expand |
Embed | Plain Text
/* * Created August 19, 2010; updated February 26, 2011 * Released into the public domain by Josh Atkins */ var dragEnabled, curDragElmnt, curDropLoc, curCursorX, curCursorY, cursorX, cursorY, positionStyles; function get(elmnt) { return document.getElementById(elmnt); } function beginDrag(dragElmnt, dropLocation, dragEvent) { if(curDragElmnt) get(curDragElmnt).style.zIndex = '1'; curDragElmnt = dragElmnt; dragEnabled = 1; get(curDragElmnt).style.zIndex = '2'; if(dropLocation&&dropLocation!='') curDropLoc = dropLocation; updateCursorPos(dragEvent); return false; } function updateCursorPos(moveEvent) { curCursorX = moveEvent.clientX + document.body.scrollLeft - document.body.clientLeft; curCursorY = moveEvent.clientY + document.body.scrollTop - document.body.clientTop; } function drag(dragEvent) { cursorX = dragEvent.clientX + document.body.scrollLeft - document.body.clientLeft; cursorY = dragEvent.clientY + document.body.scrollTop - document.body.clientTop; if(dragEnabled==1) { get(curDragElmnt).style.left = get(curDragElmnt).offsetLeft + cursorX - curCursorX + 'px'; get(curDragElmnt).style.top = get(curDragElmnt).offsetTop + cursorY - curCursorY + 'px'; } updateCursorPos(dragEvent); } function stopDrag() { if(dragEnabled==1) { curCursorX = null; curCursorY = null; if(curDropLoc&&!checkIfElmntInsideAnother(curDragElmnt, curDropLoc)) { positionStyles = get(curDragElmnt).className.substring(get(curDragElmnt).className.indexOf('posStart')+8, get(curDragElmnt).className.indexOf('posEnd')+6).split('px'); get(curDragElmnt).style.left = positionStyles[0] + 'px'; get(curDragElmnt).style.top = positionStyles[1] + 'px'; } curDropLoc = null; dragEnabled = 0; } } function trimSpaces(value) { value = value.substring(0, 1) == ' ' ? value.substring(1) : value; return value.substring(value.length-1) == ' ' ? value.substring(0, value.length-1) : value; } function enableDragDrop(dragObjs, dropLocation) { if(document.body.className.indexOf('dragDropEnabled')==-1) { document.body.className = trimSpaces(document.body.className + ' dragDropEnabled'); document.body.setAttribute('onmousemove', trimSpaces((document.body.getAttribute('onmousemove')||'') + ' drag(event);')); document.body.setAttribute('onmouseup', trimSpaces((document.body.getAttribute('onmouseup')||'') + ' stopDrag();')); } for(var i=0;i<dragObjs.length;i++) { if(get(dragObjs[i]).className.indexOf('dragDropEnabled')==-1) { get(dragObjs[i]).setAttribute('onmousedown', trimSpaces((get(dragObjs[i]).getAttribute('onmousedown')||'') + ' return beginDrag(this.id, '+(dropLocation!=null&&dropLocation!=''?'\''+dropLocation+'\'':'null')+', event);')); get(dragObjs[i]).className = trimSpaces(get(dragObjs[i]).className+' posStart'+get(dragObjs[i]).offsetLeft+'px'+get(dragObjs[i]).offsetTop+'pxposEnd dragDropEnabled'); if(!get(dragObjs[i]).style.width) get(dragObjs[i]).style.width = get(dragObjs[i]).offsetWidth + 'px'; get(dragObjs[i]).style.left = get(dragObjs[i]).offsetLeft + 'px'; get(dragObjs[i]).style.top = get(dragObjs[i]).offsetTop + 'px'; } } for(i=0;i<dragObjs.length;i++) { if(get(dragObjs[i]).style.position!='absolute') get(dragObjs[i]).style.position = 'absolute'; } } function disableDragDrop(dragObjs) { for(var i=0;i<dragObjs.length;i++) { get(dragObjs[i]).setAttribute('onmousedown', trimSpaces(get(dragObjs[i]).getAttribute('onmousedown').replace(/return beginDrag([^\)]+)\);/, ''))); get(dragObjs[i]).className = trimSpaces(get(dragObjs[i]).className.replace(/dragDropEnabled/, '')); } } function checkIfElmntInsideAnother(first, second) { return get(first).offsetTop+get(first).offsetHeight<=get(second).offsetTop+get(second).offsetHeight&& get(first).offsetLeft+get(first).offsetWidth<=get(second).offsetLeft+get(second).offsetWidth&& get(first).offsetTop>=get(second).offsetTop&&get(first).offsetLeft>=get(second).offsetLeft; }
You need to login to post a comment.
