Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. <div class="demo">
  2. <p>Available Boxes (click to select multiple boxes)</p>
  3. <ul id="draggable">
  4. <li>Box #1</li>
  5. <li>Box #2</li>
  6. <li>Box #3</li>
  7. <li>Box #4</li>
  8. </ul>
  9.  
  10. <p>My Boxes</p>
  11. <ul id="droppable">
  12. </ul>
  13.  
  14. </div>
  15.  
  16. $(document).ready(function(){
  17.  
  18. var selectedClass = 'ui-state-highlight',
  19. clickDelay = 600, // click time (milliseconds)
  20. lastClick, diffClick; // timestamps
  21.  
  22. $("#draggable li")
  23. // Script to deferentiate a click from a mousedown for drag event
  24. .bind('mousedown mouseup', function(e){
  25. if (e.type=="mousedown") {
  26. lastClick = e.timeStamp; // get mousedown time
  27. } else {
  28. diffClick = e.timeStamp - lastClick;
  29. if ( diffClick < clickDelay ) {
  30. // add selected class to group draggable objects
  31. $(this).toggleClass(selectedClass);
  32. }
  33. }
  34. })
  35. .draggable({
  36. revertDuration: 10, // grouped items animate separately, so leave this number low
  37. containment: '.demo',
  38. start: function(e, ui) {
  39. ui.helper.addClass(selectedClass);
  40. },
  41. stop: function(e, ui) {
  42. // reset group positions
  43. $('.' + selectedClass).css({ top:0, left:0 });
  44. },
  45. drag: function(e, ui) {
  46. // set selected group position to main dragged object
  47. // this works because the position is relative to the starting position
  48. $('.' + selectedClass).css({
  49. top : ui.position.top,
  50. left: ui.position.left
  51. });
  52. }
  53. });
  54.  
  55. $("#droppable, #draggable")
  56. .sortable()
  57. .droppable({
  58. drop: function(e, ui) {
  59. $('.' + selectedClass)
  60. .appendTo($(this))
  61. .add(ui.draggable) // ui.draggable is appended by the script, so add it after
  62. .removeClass(selectedClass)
  63. .css({ top:0, left:0 });
  64. }
  65. });
  66.  
  67. });
  68.  
  69. <ul>
  70. <li>One</li>
  71. <li>Two</li>
  72. <li>Three</li>
  73. </ul>
  74. <ul>
  75. <li>Four</li>
  76. <li>Five</li>
  77. <li>Six</li>
  78. </ul>
  79.  
  80. $("ul").on('click', 'li', function (e) {
  81. if (e.ctrlKey || e.metaKey) {
  82. $(this).toggleClass("selected");
  83. } else {
  84. $(this).addClass("selected").siblings().removeClass('selected');
  85. }
  86. }).sortable({
  87. connectWith: "ul",
  88. delay: 150, //Needed to prevent accidental drag when trying to select
  89. revert: 0,
  90. helper: function (e, item) {
  91. var helper = $('<li/>');
  92. if (!item.hasClass('selected')) {
  93. item.addClass('selected').siblings().removeClass('selected');
  94. }
  95. var elements = item.parent().children('.selected').clone();
  96. item.data('multidrag', elements).siblings('.selected').remove();
  97. return helper.append(elements);
  98. },
  99. stop: function (e, info) {
  100. info.item.after(info.item.data('multidrag')).remove();
  101. }
  102.  
  103. });
  104.  
  105. <style>
  106. ul {border:1px solid Black;width:200px;height:200px;display:inline-block;vertical-align:top}
  107. li {background-color:Azure;border-bottom:1px dotted Gray}
  108. li.selected {background-color:GoldenRod}
  109. </style>
  110. <h1>Click items to select them</h1>
  111. <ul>
  112. <li>One</li>
  113. <li>Two<li>
  114. <li>Three</li>
  115. </ul><ul>
  116. <li>Four</li>
  117. <li>Five<li>
  118. <li>Six</li>
  119. </ul>
  120. <script>
  121. $("li").click(function(){
  122. $(this).toggleClass("selected");
  123. })
  124. $("ul").sortable({
  125. connectWith: "ul",
  126. start:function(e,info){
  127. // info.item.siblings(".selected").appendTo(info.item);
  128. info.item.siblings(".selected").not(".ui-sortable-placeholder").appendTo(info.item);
  129.  
  130. },
  131. stop:function(e,info){
  132. info.item.after(info.item.find("li"))
  133. }
  134. })
  135. </script>
  136.  
  137. $('ul.sortable').multisortable();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement