js拖拽div例子

客官°小女子只卖身不卖艺 2021-11-01 03:34 447阅读 0赞

1.demo.html

-———————————————-

<!doctype html>



  1. <head>
  2. <style type="text/css">
  3. .taskdiv\{
  4. background-color:rgb(248, 225, 64);
  5. height:50px;
  6. width:50px;
  7. padding:0px;
  8. margin: 2px;
  9. \}
  10. .whiteboardtable \{
  11. border-collapse: collapse;
  12. border: 1px solid rgb(220, 220, 220);
  13. width: 100%;
  14. \}
  15. .whiteboardtable tr td \{
  16. -moz-border-bottom-colors: none;
  17. -moz-border-left-colors: none;
  18. -moz-border-right-colors: none;
  19. -moz-border-top-colors: none;
  20. border-color: \#DCDCDC;
  21. border-image: none;
  22. border-right: 1px solid \#DCDCDC;
  23. border-style: solid;
  24. border-width: 0 1px 1px 0;
  25. \}
  26. .whole\_outter\_tabletd \{
  27. width: 19%;
  28. position: relative;/\*\*重要\*\*/
  29. vertical-align: top;
  30. padding: 1px;
  31. font-size: 12px;
  32. \}
  33. </style>
  34. <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
  35. <script src="drag.js"></script>
  36. <script type="text/javascript">
  37. $(function()\{
  38. prepareForMove();
  39. //第二列宽度\*4
  40. var maxWait = html("whiteboardtable").rows\[0\].cells\[1\].offsetWidth\*4;
  41. console.log(maxWait);
  42. $('\#drag1').dragDrop(\{fixarea:\[0,maxWait,0,0\],callback:function(params)\{
  43. //$('\#span1').text('X:'+params.moveX+' Y:'+params.moveY);
  44. console.log(params);//这里可以直接获得移动div的ID
  45. var oddtriger=trigerStatuWindow('wait','odd',params);
  46. console.log(oddtriger);
  47. if(oddtriger=='no')\{
  48. //同一状态内的拖动,复原位置
  49. console.log("wait->wait,我要复原");
  50. revertToOri(params.myele.context.id);
  51. \}else if(oddtriger=='doing')\{
  52. //从wait->doing状态移动,这里可能要与后台交互
  53. //做些你想做的事情,如与后台交互,重新返回数据,重新刷新任务列表等
  54. console.log("从wait->doing状态移动");
  55. \}
  56. \}\});
  57. $('\#drag11').dragDrop(\{fixarea:\[0,600,0,0\],callback:function(params)\{
  58. //console.log(params.myele.context.id);这里可以直接获得移动div的ID
  59. \}\});
  60. $('\#drag2').dragDrop(\{fixarea:\[-300,400,0,0\],callback:function(params)\{
  61. //console.log(params.myele.context.id);这里可以直接获得移动div的ID
  62. \}\});
  63. $('\#drag3').dragDrop(\{fixarea:\[0,600,0,0\],callback:function(params)\{
  64. //console.log(params.myele.context.id);这里可以直接获得移动div的ID
  65. \}\});
  66. $('\#drag4').dragDrop(\{fixarea:\[0,600,0,0\],callback:function(params)\{
  67. //console.log(params.myele.context.id);这里可以直接获得移动div的ID
  68. \}\});
  69. \});
  70. function html(id)\{return document.getElementById(id)\}
  71. </script>
  72. </head>
  73. <body>
  74. <table id="whiteboardtable" width="100%" class="whiteboardtable">
  75. <tr><td width="238">需求</td><td width="238">状态1</td><td width="238">状态2</td><td width="238">状态3</td><td width="238">状态4</td></tr>
  76. <tr>
  77. <td id="firsttd1" class="half\_outter\_tabletd">
  78. <div>需求1</div>
  79. </td>
  80. <td id="179wait\_td" class="whole\_outter\_tabletd">
  81. <table>
  82. <tr>
  83. <td class="whole\_outter\_tabletd"><div id="drag1" class="taskdiv" οnmοusedοwn="divmoveUp(this.id)">任务1</div></td>
  84. <td class="whole\_outter\_tabletd"><div id="drag11" class="taskdiv" οnmοusedοwn="divmoveUp(this.id)">任务11</div></td>
  85. </tr>
  86. <tr>
  87. <td class="whole\_outter\_tabletd"><div id="drag3" class="taskdiv" οnmοusedοwn="divmoveUp(this.id)">任务3</div></td>
  88. <td class="whole\_outter\_tabletd"><div id="drag4" class="taskdiv" οnmοusedοwn="divmoveUp(this.id)">任务4</div></td>
  89. </tr>
  90. </table>
  91. </td>
  92. <td class="whole\_outter\_tabletd">&nbsp;</td>
  93. <td class="whole\_outter\_tabletd">&nbsp;</td>
  94. <td class="whole\_outter\_tabletd">&nbsp;</td>
  95. </tr>
  96. <tr>
  97. <td><div>需求2</div></td>
  98. <td class="whole\_outter\_tabletd"></td>
  99. <td class="whole\_outter\_tabletd">&nbsp;</td>
  100. <td class="whole\_outter\_tabletd"><div id="drag2" class="taskdiv" οnmοusedοwn="divmoveUp(this.id)">任务2</div></td>
  101. <td class="whole\_outter\_tabletd">&nbsp;</td>
  102. </tr>
  103. </table>
  104. <span id="span1"></span>
  105. </body>

-———————————————-

2.drag.js

-———————————————-

/**
* 移动的准备工作
*/
function prepareForMove() {
$.extend({
// 获取鼠标当前坐标
mouseCoords : function(ev) {
if (ev.pageX || ev.pageY) {
return {
x : ev.pageX,
y : ev.pageY
};
}
return {
x : ev.clientX + document.body.scrollLeft

  1. - document.body.clientLeft,
  2. y : ev.clientY + document.body.scrollTop
  3. - document.body.clientTop
  4. \};
  5. \},
  6. // 获取样式值
  7. getStyle : function(obj, styleName) \{
  8. return obj.currentStyle ? obj.currentStyle\[styleName\]
  9. : document.defaultView.getComputedStyle(obj, null)\[styleName\];
  10. // return obj.currentStyle ? obj.currentStyle\[styleName\] :
  11. // document.defaultView.getComputedStyle(obj,null).getPropertyValue(styleName);
  12. \}
  13. \});
  14. // 元素拖拽插件
  15. $.fn.dragDrop = function(options) \{
  16. var opts = $.extend(\{\}, $.fn.dragDrop.defaults, options);
  17. return this.each(function() \{
  18. // 是否正在拖动
  19. var bDraging = false;
  20. // 移动的元素
  21. var moveEle = $(this);
  22. // 点击哪个元素,以触发移动。
  23. // 该元素需要是被移动元素的子元素(比如标题等)
  24. var focuEle = opts.focuEle ? $(opts.focuEle, moveEle) : moveEle;
  25. if (!focuEle || focuEle.length <= 0) \{
  26. alert('focuEle is not found! the element must be a child of '
  27. + this.id);
  28. return false;
  29. \}
  30. // initDiffX|Y : 初始时,鼠标与被移动元素原点的距离
  31. // moveX|Y : 移动时,被移动元素定位位置 (新鼠标位置与initDiffX|Y的差值)
  32. // 如果定义了移动中的回调函数,该对象将以参数传入回调函数。
  33. var dragParams = \{
  34. initDiffX : '',
  35. initDiffY : '',
  36. moveX : '',
  37. moveY : '',
  38. myele : ''
  39. \};
  40. // 被移动元素,需要设置定位样式,否则拖拽效果将无效。
  41. moveEle.css(\{
  42. 'position' : 'relative',
  43. 'left' : '0',
  44. 'top' : '0'
  45. \});
  46. dragParams.myele = moveEle;
  47. // 点击时,记录鼠标位置
  48. // DOM写法: getElementById('\*\*\*').οnmοusedοwn= function(event);
  49. var drag = function(e) \{
  50. e = e || window.event;
  51. //focuEle.style.cursor = "pointer";
  52. !+"\\v1"? document.selection.empty() : window.getSelection().removeAllRanges();
  53. //focuEle.style.left = e.clientX - focuEle.offset\_x + "px";
  54. //focuEle.style.top = e.clientY - focuEle.offset\_y + "px";
  55. //focuEle.innerHTML = parseInt(focuEle.style.left,10)+ "X"+parseInt(focuEle.style.top,10);
  56. if (bDraging) \{
  57. // if(moveEle.get(0).setCapture)
  58. // \{
  59. // moveEle.get(0).setCapture();
  60. // \}
  61. moveEle.css(\{
  62. 'cursor' : 'move'
  63. \});
  64. // 被移动元素的新位置,实际上鼠标当前位置与原位置之差
  65. // 实际上,被移动元素的新位置,也可以直接是鼠标位置,这也能体现拖拽,但是元素的位置就不会精确。
  66. dragParams.moveX = $.mouseCoords(e).x
  67. - dragParams.initDiffX;
  68. dragParams.moveY = $.mouseCoords(e).y
  69. - dragParams.initDiffY;
  70. // 是否限定在某个区域中移动.
  71. // fixarea格式: \[x轴最小值,x轴最大值,y轴最小值,y轴最大值\]
  72. if (opts.fixarea) \{
  73. if (dragParams.moveX < opts.fixarea\[0\]) \{
  74. dragParams.moveX = opts.fixarea\[0\]
  75. \}
  76. if (dragParams.moveX > opts.fixarea\[1\]) \{
  77. dragParams.moveX = opts.fixarea\[1\]
  78. \}
  79. if (dragParams.moveY < opts.fixarea\[2\]) \{
  80. dragParams.moveY = opts.fixarea\[2\]
  81. \}
  82. if (dragParams.moveY > opts.fixarea\[3\]) \{
  83. dragParams.moveY = opts.fixarea\[3\]
  84. \}
  85. \}
  86. // 移动方向:可以是不限定、垂直、水平。
  87. if (opts.dragDirection == 'all') \{
  88. // DOM写法: document.getElementById('\*\*\*').style.left =
  89. // '\*\*\*px';
  90. moveEle.css(\{
  91. 'left' : dragParams.moveX,
  92. 'top' : dragParams.moveY
  93. \});
  94. \} else if (opts.dragDirection == 'vertical') \{
  95. moveEle.css(\{
  96. 'top' : dragParams.moveY
  97. \});
  98. \} else if (opts.dragDirection == 'horizontal') \{
  99. moveEle.css(\{
  100. 'left' : dragParams.moveX
  101. \});
  102. \}
  103. // 如果有回调
  104. // if(opts.callback)
  105. // \{
  106. // 将dragParams作为参数传递
  107. // opts.callback.call(a,1);
  108. // \}
  109. \}
  110. \}
  111. var dragend = function()\{
  112. document.onmouseup = null;
  113. document.onmousemove = null;
  114. bDraging = false;
  115. moveEle.css(\{
  116. 'cursor' : 'default'
  117. \});
  118. // if(moveEle.get(0).releaseCapture && dragParams.moveX!=0)
  119. if (dragParams.moveX != 0) \{
  120. if (opts.callback) \{
  121. // 将dragParams作为参数传递
  122. opts.callback.call(opts.callback, dragParams);
  123. \}
  124. \}
  125. if (moveEle.get(0).releaseCapture) \{
  126. moveEle.get(0).releaseCapture();
  127. \} else \{
  128. // window.releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP);
  129. \}
  130. \}
  131. var dragstart = function(e)\{
  132. e = e || window.event;
  133. focuEle.offset\_x = e.clientX - focuEle.offsetLeft;
  134. focuEle.offset\_y = e.clientY - focuEle.offsetTop;
  135. document.onmouseup = dragend;
  136. document.onmousemove = drag;
  137. // 标记开始移动
  138. bDraging = true;
  139. // 改变鼠标形状
  140. moveEle.css(\{
  141. 'cursor' : 'default'
  142. \});
  143. // 捕获事件。(该用法,还有个好处,就是防止移动太快导致鼠标跑出被移动元素之外)
  144. if (moveEle.get(0).setCapture) \{
  145. moveEle.get(0).setCapture();
  146. \} else \{
  147. // window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
  148. \}
  149. dragParams.initDiffX = $.mouseCoords(e).x
  150. - moveEle.position().left;
  151. dragParams.initDiffY = $.mouseCoords(e).y
  152. - moveEle.position().top;
  153. return false;
  154. \}
  155. //focuEle.onmousedown = dragstart;
  156. focuEle.bind('mousedown',dragstart);
  157. \});
  158. \};
  159. // 默认配置
  160. $.fn.dragDrop.defaults = \{
  161. focuEle : null, // 点击哪个元素开始拖动,可为空。不为空时,需要为被拖动元素的子元素。
  162. callback : null, // 拖动时触发的回调。
  163. dragDirection : 'all', // 拖动方向:\['all','vertical','horizontal'\]
  164. fixarea : null
  165. // 限制在哪个区域拖动,以数组形式提供\[minX,maxX,minY,maxY\]
  166. \};

}

/**
* 通过移动的位置和移动的statu块,计算移动到的位置返回 wait,doing,done,closed location : odd 奇数 even 偶数
*/
function trigerStatuWindow(statu, location, params) {
var tablewidth = parseInt(parseInt(window.innerWidth
|| document.documentElement.clientWidth) - 79)*0.95;
var waitTdWidth = tablewidth * 0.2;
switch (statu) {
case “wait”:
if (location == ‘odd’) {
if (0 <= params.moveX && params.moveX < 3 / 4 * waitTdWidth) {
return ‘no’;
} else if (3 / 4 * waitTdWidth <= params.moveX
&& params.moveX < 7 / 4 * waitTdWidth) {
return ‘doing’;
} else if (7 / 4 * waitTdWidth <= params.moveX
&& params.moveX < 11 / 4 * waitTdWidth) {
return ‘done’;
} else if (11 / 4 * waitTdWidth <= params.moveX
&& params.moveX < 15 / 4 * waitTdWidth) {
return ‘test’;
} else if (15 / 4 * waitTdWidth <= params.moveX) {
return ‘closed’;
} else {
return ‘no’;
}
} else {
if (0 <= params.moveX && params.moveX < 1 / 4 * waitTdWidth) {
return ‘no’;
} else if (1 / 4 * waitTdWidth < params.moveX
&& params.moveX < 5 / 4 * waitTdWidth) {
return ‘doing’;
} else if (5 / 4 * waitTdWidth <= params.moveX
&& params.moveX < 9 / 4 * waitTdWidth) {
return ‘done’;
} else if (9 / 4 * waitTdWidth <= params.moveX
&& params.moveX < 13 / 4 * waitTdWidth) {
return ‘test’;
} else if (13 / 4 * waitTdWidth <= params.moveX) {
return ‘closed’;
} else {
return ‘no’;
}
}
case “doing”:
if (location == ‘odd’) {
if (-1 / 4 * waitTdWidth >= params.moveX) {
return ‘wait’;
} else if (-1 / 4 * waitTdWidth < params.moveX
&& params.moveX < 3 / 4 * waitTdWidth) {
return ‘no’;
} else if (3 / 4 * waitTdWidth <= params.moveX
&& params.moveX < 7 / 4 * waitTdWidth) {
return ‘done’;
} else if (7 / 4 * waitTdWidth <= params.moveX
&& params.moveX < 11 / 4 * waitTdWidth) {
return ‘test’;
} else if (11 / 4 * waitTdWidth <= params.moveX) {
return ‘closed’;
} else {
return ‘no’;
}
} else {
if (-3 / 4 * waitTdWidth >= params.moveX) {
return ‘wait’;
} else if (-3 / 4 * waitTdWidth < params.moveX
&& params.moveX < 1 / 4 * waitTdWidth) {
return ‘no’;
} else if (1 / 4 * waitTdWidth < params.moveX
&& params.moveX < 5 / 4 * waitTdWidth) {
return ‘done’;
} else if (5 / 4 * waitTdWidth < params.moveX
&& params.moveX < 9 / 4 * waitTdWidth) {
return ‘test’;
} else if (9 / 4 * waitTdWidth <= params.moveX) {
return ‘closed’;
} else {
return ‘no’;
}
}
break;
case “done”:
if (location == ‘odd’) {
if (-5 / 4 * waitTdWidth >= params.moveX) {
return ‘wait’;
} else if (-5 / 4 * waitTdWidth < params.moveX
&& params.moveX <= -1 / 4 * waitTdWidth) {
return ‘doing’;
} else if (-1 / 4 * waitTdWidth < params.moveX
&& params.moveX < 3 / 4 * waitTdWidth) {
return ‘no’;
} else if (3 / 4 * waitTdWidth <= params.moveX
&& params.moveX < 7 / 4 * waitTdWidth) {
return ‘test’;
} else if (7 / 4 * waitTdWidth <= params.moveX) {
return ‘closed’;
} else {
return ‘no’;
}
} else {
if (-7 / 4 * waitTdWidth >= params.moveX) {
return ‘wait’;
} else if (-7 / 4 * waitTdWidth < params.moveX
&& params.moveX < -3 / 4 * waitTdWidth) {
return ‘doing’;
} else if (-3 / 4 * waitTdWidth < params.moveX
&& params.moveX < 1 / 4 * waitTdWidth) {
return ‘no’;
} else if (1 / 4 * waitTdWidth <= params.moveX
&& params.moveX < 5 / 4 * waitTdWidth) {
return ‘test’;
} else if (5 / 4 * waitTdWidth <= params.moveX) {
return ‘closed’;
} else {
return ‘no’;
}
}
break;
case “test”:
if (location == ‘odd’) {
if (-9 / 4 * waitTdWidth >= params.moveX) {
return ‘wait’;
} else if (-9 / 4 * waitTdWidth < params.moveX
&& params.moveX <= -5 / 4 * waitTdWidth) {
return ‘doing’;
} else if (-5 / 4 * waitTdWidth < params.moveX
&& params.moveX <= -1 / 4 * waitTdWidth) {
return ‘done’;
}else if (-1 / 4 * waitTdWidth < params.moveX
&& params.moveX < 3 / 4 * waitTdWidth) {
return ‘no’;
} else if (3 / 4 * waitTdWidth <= params.moveX) {
return ‘closed’;
} else {
return ‘no’;
}
} else {
if (-11 / 4 * waitTdWidth >= params.moveX) {
return ‘wait’;
} else if (-11 / 4 * waitTdWidth < params.moveX
&& params.moveX <= -7 / 4 * waitTdWidth) {
return ‘doing’;
} else if (-7 / 4 * waitTdWidth < params.moveX
&& params.moveX <= -3 / 4 * waitTdWidth) {
return ‘done’;
}else if (-3 / 4 * waitTdWidth < params.moveX
&& params.moveX < 1 / 4 * waitTdWidth) {
return ‘no’;
} else if (1 / 4 * waitTdWidth <= params.moveX) {
return ‘closed’;
} else {
return ‘no’;
}
}
break;
case “closed”:
if (-1 / 4 * waitTdWidth < params.moveX) {
return ‘no’;
} else if (-5 / 4 * waitTdWidth < params.moveX
&& params.moveX < -1 / 4 * waitTdWidth) {
return ‘test’;
} else if (-9 / 4 * waitTdWidth < params.moveX
&& params.moveX < -5 / 4 * waitTdWidth) {
return ‘done’;
} else if (-13 / 4 * waitTdWidth < params.moveX
&& params.moveX < -9 / 4 * waitTdWidth) {
return ‘doing’;
} else if (-13 / 4 * waitTdWidth >= params.moveX) {
return ‘wait’;
} else {
return ‘no’;
}
break;
}
}

/**
* div复位
*/
function revertToOri(elemId) {
html(elemId).style.left = 0 + ‘px’;
html(elemId).style.zIndex = 0;
}

/**
* div置上
*/
function divmoveUp(id)
{
var box = document.getElementById(id);
box.style.zIndex=1;
}

-———————————————

发表评论

表情:
评论列表 (有 0 条评论,447人围观)

还没有评论,来说两句吧...

相关阅读

    相关 js

    拖拽使用的三个事件 onmousedown //鼠标按下 onmousemove //鼠标移动 onmouseup //鼠标抬起

    相关 JS

    实现后效果 ![70][] 实现原理: 1. 在允许拖拽的节点元素上,监听mousedown(按下鼠标按钮)事件,鼠标按下后,事件触发 2. 监听mousemov