Question

How can I drag an element only to the left and only for X pixels?

Obviously using jQuery UI Draggable.

I've tried something like

$('li').draggable({
    axis: 'x',
    containment: [0,0,-250,250]
});

But it doesn't do what I want..

EDIT: JSFiddle: http://jsfiddle.net/MJvn8/

Was it helpful?

Solution

It can be done with something like:

var startPosition = 0;
$('li').draggable({
    axis: 'x',
    start: function( event, ui ) {
        startPosition = ui.position.left;
    },
    drag: function( event, ui ) {
        if(ui.position.left > startPosition)
            ui.position.left = startPosition;
        if(ui.position.left < -250)
            ui.position.left = -250;
        startPosition = ui.position.left;
    }
});

Check This Example

Update

To allow a user to get back to the original position Link

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top