Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery draggable scrollable container

I currently have a simple draggable list, which I want to be inside a div ( the items ) and scrollable.

$( ".draggable" ).draggable();

I currently have this fiddle: http://jsfiddle.net/YvJhE/660/

But as you can see, the draggable elements stay inside the elements container, I want them to be able to drag out, but also the ability to scroll inside the elements container as soon as there are more elements, than the container long is.

Can anyone get me on the right track?

like image 997
Dennis Smink Avatar asked Sep 03 '15 08:09

Dennis Smink


People also ask

How to use jQuery UI draggable?

In this article, we learnt about the jQuery UI draggable functionality which basically allows the DOM elements to be moved or dragged using the mouse. This is done by clicking on the object to be dragged using mouse and dragging it anywhere within the view port.

How do I move a draggable object in HTML?

Allow elements to be moved using the mouse. Enable draggable functionality on any DOM element. Move the draggable object by clicking on it with the mouse and dragging it anywhere within the viewport. Want to learn more about the draggable interaction? Check out the API documentation.

How to enable draggable functionality on any DOM element?

Enable draggable functionality on any DOM element. Move the draggable object by clicking on it with the mouse and dragging it anywhere within the viewport.

How to move the DOM elements using the mouse in jQuery?

jQueryUIdraggable () method allows the DOM elements to be moved using the mouse. This method can be used to drag the DOM elements anywhere within the view port by clicking on the object using mouse and then dragging it to anywhere you want to within the view port.


2 Answers

jQuery handles interactions like these with a helper object that you can append to another DOM element:

$( ".draggable" ).draggable({
    helper: 'clone',
    appendTo: '#outer'
});

Then you can have a droppable somewhere:

$('#dropspace').droppable({
    accept: '.draggable',
    drop: function( event, ui ) {
        window.alert('Element dropped at left: ' + ui.position.left + '; top: ' + ui.position.top);
    }
});

Proof of concept: http://jsfiddle.net/YvJhE/662/

You still need to move the actual dom element to where it was dropped, or recreate it depending on what you're going to use it for.

like image 120
Joakim Johansson Avatar answered Oct 22 '22 00:10

Joakim Johansson


The option helper: 'clone' makes a clone of the item you're dragging automatically so that it can be dragged out of the container:

jQuery(".draggable").draggable({
    helper: 'clone',
    revert: 'invalid',
    appendTo: 'body'
});
like image 1
Madalina Taina Avatar answered Oct 21 '22 22:10

Madalina Taina