Although the JSuar solution works, I also want to try.
Solving the problem with jQuery UI Draggable Widget is not possible.
There is no explicit function to get information about moving an object vertically or horizontally. In fact, in most cases, the object moves in both directions . But we could define our own function to see how the cursor moved.
Use a custom function to get information about a draggable object.
The idea is to fire the event on mousemove and check the cursor position each time. After a certain interval of mousemovements, we check the overall movement to set the scroll. In this case, we simply assume that the user is likely to move in one direction for the following movements.
Here is a working JSFiddle example.
HTML
<div id="viewPort"> <div id="draggable">drag me</div> </div>
CSS
#viewPort { width: 300px; height: 300px; background: #fff; overflow:scroll; border:1px solid #ccc; } #draggable { color: #fff; width: 75px; height: 75px; padding: 10px; background: #333399; border:1px solid #ccc; margin:5px; }
Js
//current count of how often we got the mouse positon var count = 0; //position of mouse at last function call var lastPositionX; var lastPositionY; //how often we moved in one direction in the last intervall var movedX; var movedY; //intervall to check movement again var countIntervall = 30; $("#draggable").draggable({ scrollSensitivity: 1000, scrollSpeed: 40, revert: function (event, ui) { $("#draggable").originalPosition = { top: 0, left: 0 }; return !event; }, drag: function (e) { setScrollSensivity(); } }); //init first mousepostion $("#draggable").on('click', function() { lastPositionX = event.pageX; lastPositionY = event.pageY; }); function setScrollSensivity(){ count++; //get current mousepostion var currentPositionX = event.pageX; var currentPositionY = event.pageY; //increase if moved on chose axis if ( currentPositionX != lastPositionX ){ movedX++; } if ( currentPositionY != lastPositionY ){ movedY++; } //if moved more on x axis decrease sensivity, else increase. if (count == countIntervall){ if (movedX > movedY){ console.log("moving on X axis"); $( "#draggable" ).draggable( "option", "scrollSensitivity", 10 ); } else { console.log("moving on Y axis"); $( "#draggable" ).draggable( "option", "scrollSensitivity", 200 ); } //reset all the counters and start again count = 0; movedX = 0; movedY = 0; } //set last position for our next function call lastPositionX = currentPositionX; lastPositionY = currentPositionY; }
source share