| <! Doctype html> <Html> <Head> <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/> <Title> drag rows </title> <Script> Window. onload = function (){ // Bind events Var addEvent = document. addEventListener? Function (el, type, callback ){ El. addEventListener (type, callback ,! 1 ); }: Function (el, type, callback ){ El. attachEvent ("on" + type, callback ); } // Determine the style support Var getStyleName = (function (){ Var prefixes = ['', '-ms-', '-moz-', '-webkit-', '-khtml-', '-o-']; Var reg_cap =/-([a-z])/g; Function getStyleName (css, el ){ El = el | document.doc umentElement; Var style = el. style, test; For (var I = 0, l = prefixes. length; I <l; I ++ ){ Test = (prefixes [I] + css). replace (reg_cap, function ($0, $1 ){ Return $1. toUpperCase (); }); If (test in style ){ Return test; } } Return null; } Return getStyleName; })(); Var userSelect = getStyleName ("user-select "); // Obtain the style accurately Var getStyle = document. defaultView? Function (el, style ){ Return document. defaultView. getComputedStyle (el, null). getPropertyValue (style) }: Function (el, style ){ Style = style. replace (/\-(\ w)/g, function ($, $1 ){ Return $1. toUpperCase (); }); Return el. currentStyle [style]; } Var dragManager = { Y: 0, DragStart: function (e ){ E = e | event; Var handler = e.tar get | e. srcElement; If (handler. nodeName = "TD "){ Handler = handler. parentNode; DragManager. handler = handler; If (! Handler. getAttribute ("data-background ")){ Handler. setAttribute ("data-background", getStyle (handler, "background-color ")) } // Display as movable Handler. style. backgroundColor = "# ccc "; Handler. style. cursor = "move "; DragManager. y = e. clientY; If (typeof userSelect = "string "){ Return document.doc umentElement. style [userSelect] = "none "; } Document. unselectable = "on "; Document. onselectstart = function (){ Return false; } } }, Draging: function (e) {// drag the row when mousemove Var handler = dragManager. handler; If (handler ){ E = e | event; Var y = e. clientY; Var down = y> dragManager. y; // whether to move down Var tr = document. elementFromPoint (e. clientX, e. clientY ); If (tr & tr. nodeName = "TD "){ Tr = tr. parentNode DragManager. y = y; If (handler! = Tr ){ Tr. parentNode. insertBefore (handler, (down? Tr. nextSibling: tr )); } }; } }, DragEnd: function (){ Var handler = dragManager. handler If (handler ){ Handler. style. backgroundColor = handler. getAttribute ("data-background "); Handler. style. cursor = "default "; DragManager. handler = null; } If (typeof userSelect = "string "){ Return document.doc umentElement. style [userSelect] = "text "; } Document. unselectable = "off "; Document. onselectstart = null; }, Main: function (el ){ AddEvent (el, "mousedown", dragManager. dragStart ); AddEvent (document, "mousemove", dragManager. draging ); AddEvent (document, "mouseup", dragManager. dragEnd ); } } Var el = document. getElementById ("table "); DragManager. main (el ); } </Script> <Style> . Table {width: 60%; border: 1px solid red; border-collapse: collapse ;} . Table td {border: 1px solid red; height: 20px ;} </Style> </Head> <Body> <H1> drag rows <Table id = "table" class = "table"> <Tbody> <Tr> <td> 1 </td> <td> One </td> <td> dom. require </td> </tr> <Tr id = "2"> <td class = "2"> 2 </td> <td> Two </td> <td> ControlJS </td> </tr> <Tr id = "3"> <td class = "3"> 3 </td> <td> Three </td> <td> HeadJS </td> </tr> <Tr id = "4"> <td class = "4"> 4 </td> <td> Four </td> <td> LAB. js </td> </tr> <Tr id = "5"> <td class = "5"> 5 </td> <td> Five </td> <td> $ script. js </td> </tr> <Tr id = "6"> <td class = "6"> 6 </td> <td> Six </td> <td> NBL. js </td> </tr> </Tbody> </Table> </Body> </Html> |