Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery slim scroll with jquery ui sortable issue

Hi I have a situation where I am using jquery slim scroll plugin with jquery sortable. The problem is when I drag the items from one list items to other the scroll bar on the right doesn't move along with , So if I have to drop the list to the last list items area I cannot go there unless I use mousewheel. So how can i bind scrollbar position as I drag the item from one list area to other. Below is the code-

$(function() {
   $("ul.droptrue").sortable({

     revert: 'invalid',
     connectWith: "ul"
   });


   $("#sortable1, #sortable2, #sortable3").disableSelection();


   $('.ScrollableAreanew ').slimScroll({
     height: '400',
     width: '100%',
     alwaysVisible: true,
     color: 'rgb(15,170,255)',
     railOpacity: 1,
     opacity: 1,
     size: '5px',
     position: 'right',
     allowPageScroll: false,


   });
 });
#sortable1,
#sortable2,
#sortable3 {
  list-style-type: none;
  margin: 0;
  float: left;
  margin-right: 10px;
  background: #eee;
  padding: 5px;
  width: 100%;
  margin-bottom: 10px;
}
#sortable1 li,
#sortable2 li,
#sortable3 li {
  margin: 5px;
  padding: 5px;
  font-size: 1.2em;
  width: 100%;
  border: 1px solid #000;
  margin-bottom: 5px;
}
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="http://rocha.la/misc/jsdemos/slimScroll/jquery.slimscroll.js"></script>
<div class="ScrollableAreanew">
  <ul id="sortable1" class="droptrue">
    <li class="ui-state-default">Can be dropped..</li>
    <li class="ui-state-default">..on an empty list</li>
    <li class="ui-state-default">Item 3</li>
    <li class="ui-state-default">Item 4</li>
    <li class="ui-state-default">Item 5</li>
  </ul>
  <ul id="sortable2" class="droptrue">
    <li class="ui-state-highlight">Cannot be dropped..</li>
    <li class="ui-state-highlight">..on an empty list</li>
    <li class="ui-state-highlight">Item 3</li>
    <li class="ui-state-highlight">Item 4</li>
    <li class="ui-state-highlight">Item 5</li>
  </ul>
  <ul id="sortable3" class="droptrue">
    <li class="ui-state-highlight">Cannot be dropped..</li>
    <li class="ui-state-highlight">..on an empty list</li>
    <li class="ui-state-highlight">Item 3</li>
    <li class="ui-state-highlight">Item 4</li>
    <li class="ui-state-highlight">Item 5</li>
  </ul>
</div>
like image 518
Sahil Dhir Avatar asked Feb 07 '17 06:02

Sahil Dhir


1 Answers

Here what we need to do is

  1. Get position of mouse - Y coordinate as we move inside the .ScrollableAreanew
  2. Whenever a sortable item is moved (use sort event), move the slimScroll to the scrolled position using scrollTo.

sort event is triggered during sorting. See here for more details

$(function() {
  var currentMousePos = { x: -1, y: -1 };
   $(document).mousemove(function(event) {
        currentMousePos.x = event.pageX;
        currentMousePos.y = event.pageY; // Get position of mouse - Y coordinate
   });
  
  $("ul.droptrue").sortable({
     sort : function(event, ui) { // This event is triggered during sorting.
     var scrollTo_int = currentMousePos.y + 'px';
    $(".ScrollableAreanew").slimScroll({
     scrollTo : scrollTo_int, // scroll to mouse position
     height: '400',
     width: '100%',
     alwaysVisible: true,
     color: 'rgb(15,170,255)',
     railOpacity: 1,
     opacity: 1,
     size: '5px',
     position: 'right',
     allowPageScroll: false
  });
     },
     revert: 'invalid',
     connectWith: "ul"
   });

   $("#sortable1, #sortable2, #sortable3").disableSelection();

   $('.ScrollableAreanew ').slimScroll({
     height: '400',
     width: '100%',
     alwaysVisible: true,
     color: 'rgb(15,170,255)',
     railOpacity: 1,
     opacity: 1,
     size: '5px',
     position: 'right',
     allowPageScroll: false
   });
 });
#sortable1,
#sortable2,
#sortable3 {
  list-style-type: none;
  margin: 0;
  float: left;
  margin-right: 10px;
  background: #eee;
  padding: 5px;
  width: 100%;
  margin-bottom: 10px;
}
#sortable1 li,
#sortable2 li,
#sortable3 li {
  margin: 5px;
  padding: 5px;
  font-size: 1.2em;
  width: 100%;
  border: 1px solid #000;
  margin-bottom: 5px;
}

.justAddingHeight
{
  height:100px !important;
}
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="http://rocha.la/misc/jsdemos/slimScroll/jquery.slimscroll.js"></script>
<div class="justAddingHeight"></div>
<div class="ScrollableAreanew">
  <ul id="sortable1" class="droptrue droppable">
    <li class="ui-state-default draggable">Can be dropped..</li>
    <li class="ui-state-default draggable">..on an empty list</li>
    <li class="ui-state-default draggable">Item 3</li>
    <li class="ui-state-default draggable">Item 4</li>
    <li class="ui-state-default draggable">Item 5</li>
  </ul>
  <ul id="sortable2" class="droptrue droppable">
    <li class="ui-state-highlight draggable">Cannot be dropped..</li>
    <li class="ui-state-highlight draggable">..on an empty list</li>
    <li class="ui-state-highlight draggable">Item 3</li>
    <li class="ui-state-highlight draggable">Item 4</li>
    <li class="ui-state-highlight draggable">Item 5</li>
  </ul>
  <ul id="sortable3" class="droptrue droppable">
    <li class="ui-state-highlight draggable">Cannot be dropped..</li>
    <li class="ui-state-highlight draggable">..on an empty list</li>
    <li class="ui-state-highlight draggable">Item 3</li>
    <li class="ui-state-highlight draggable">Item 4</li>
    <li class="ui-state-highlight draggable">Item 5</li>
  </ul>
</div>

EDIT

  1. Instead of getting mouse position inside the .ScrollableAreanew div, get the position of mouse in the document.

Note: Added div with class justAddingHeight (having height of 100px) as a POC.

Hope this solves your problem.

like image 112
Senjuti Mahapatra Avatar answered Sep 29 '22 22:09

Senjuti Mahapatra