Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With jquery ui draggable, is it possible to have different scrollsensitivity vertical versus horizontal?

I am using jquery ui draggable and I realized that I want the scrollsensitivity to be much smaller when I am dragging up down (versus left / right). Is this possible to have a different settings for vertical versus horizontal dragging?

Here is my current code

   $(".myDraggable").draggable(
            {
                stack: ".myDraggable",
                scroll: true,
                scrollSensitivity: 250,
                scrollSpeed: 40,
                revert: function (event, ui) {
                    $(this).data("uiDraggable").originalPosition = {
                        top: 0,
                        left: 0
                    };
                    return !event;
                }
            }
        );
like image 649
leora Avatar asked Oct 21 '14 12:10

leora


People also ask

How does jQuery ui draggable work?

Draggable() Method This method allows the elements to be dragged with the help of mouse. Using jQuery UI, we can make the DOM(Document Object Model) elements to drag anywhere within the view port. This can be done by clicking on the draggable object by mouse and dragging it anywhere within the view port.

How do I limit a draggable area?

Limit draggable area using 'containment' option It is simple. All you have to do is, add an option called containment to the draggable() method. The containment option has a value parent.

What is the syntax of the draggable function?

Syntax: $(selector, context). draggable ("action", [params]);

Why is draggable not working?

Check whether your draggable object is already loaded in the viewport. If it is not, it won't work properly. JUST AFTER the draggable object to be absolutely sure that everything is loaded at the correct time. When you'll be sure everything is ok, then you'll be able to refactor.


1 Answers

Updated Answer

Working Example: http://jsfiddle.net/22bpbsrw/3/

I think detecting when a scroll event occurrs in the viewport actually works. You can set the scroll speed and sensitivity then.

var lastScrollTop = 0,
    lastScrollLeft = 0,
    delta = 5;
$('#viewPort').scroll(function (event) {
    var st = $(this).scrollTop();
    var sl = $(this).scrollLeft();

    if (Math.abs(lastScrollTop - st) > delta) {
        $("#draggable").draggable("option", "scrollSensitivity", 5);
        $("#draggable").draggable("option", "scrollSpeed", 10);
        (st > lastScrollTop) ? console.log('scroll down') : console.log('scroll up');
        lastScrollTop = st;
    }

    if (Math.abs(lastScrollLeft - sl) > delta) {
        $("#draggable").draggable("option", "scrollSensitivity", 100);
        $("#draggable").draggable("option", "scrollSpeed", 40);
        sl > lastScrollLeft ? console.log('scroll right') : console.log('scroll left');
        lastScrollLeft = sl;
    }
});

Old Answer

Left just in case it could help others.

You cannot explicitly have different scrollSensitivity values for each direction. Maybe you could submit a ticket for that enhancement?

You might be able to change it via the drag function (based on this answer). See working example here: http://codepen.io/anon/pen/mbups?editors=001

drag: function(e) {    
  console.log($("#drag1").draggable( "option", "scrollSensitivity"));
  if(prevX == -1) { prevX = e.pageX; }
  // dragged left or right
  if(prevX > e.pageX || prevX < e.pageX) { // dragged right
      $("#drag1").draggable( "option", "scrollSensitivity", 100 );
  }
  prevX = e.pageX;
 
 if(prevY == -1) { prevY = e.pageY; } 
  // dragged up or down
  if(prevY > e.pageY || prevY < e.pageX) { // dragged down
      $("#drag1").draggable( "option", "scrollSensitivity", 1 );
  }
  prevY = e.pageY;
}

You could set the scrollSensitivity based on the direction of the dragged element.

like image 194
JSuar Avatar answered Sep 22 '22 16:09

JSuar