Search code examples
jquerydraggablecss-positiondroppableappendto

jQuery: stop absolute positioning of dragged elements?


I am a complete jQuery and Javascript noob trying to get a very simple drag and drop live demo working. I am getting close but my dragged elements are given absolute positioning, so they don't flow nicely together.

Markup is very simple:

<section class="favrecentboxessection" id="favorites"> 

    little divs with the style set to draggable 

</section> 

<section class="favrecentboxessection" id="recents"> 

    where they are dragged to

</section> 

And my sad little jQuery:

        $( init );

    var $favorites = $( "#favorites" ),
        $recent = $( "#recent" );


    function init() {
        $('.favrecentbox').draggable( {
            cursor: 'move',
            containment: '#mainsection',
            stack: '.favrecentbox',
            revert: 'invalid',
            revertDuration: 200,
        } );

            $('.favrecentboxessection').droppable( {
            accept: ".favrecentbox",
            activeClass: 'ui-state-highlight',
                drop: function (event, ui) {
                handleDropEvent (ui.draggable);
                }
        } );

        function handleDropEvent( $item ) {

        var temp;
        temp = $item.detach();
        temp.appendTo( $('#favorites'));

I've got the dragging working, but when dropped they carry over absolute positioning (left, top, etc are set). How do I turn that off?


Solution

  • you could add it as a child to the droppable container when dropped.

    $('.favrecentboxessection').droppable( {
            accept: ".favrecentbox",
            drop: function(ev, ui) {
                $(this).append($(ui.draggable));
                $(ui.draggable).css({position:'relative'});
            },
            activeClass: 'ui-state-highlight',
                drop: function (event, ui) {
                handleDropEvent (ui.draggable);
                }
        } );