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;
}
}
);
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.
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.
Syntax: $(selector, context). draggable ("action", [params]);
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.
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;
}
});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With