Reputation: 2107
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?
Upvotes: 0
Views: 1992
Reputation: 13134
No need to make it more complicated than it already is. Removing CSS seems like a bad idea.
See: http://jsfiddle.net/MPy9n/6/
draggableOptions = { appendTo: "body", zIndex: 99};
$( ".favrecentbox" ).draggable(draggableOptions);
$( ".favrecentboxessection" ).droppable({
activeClass: "ui-state-default",
hoverClass: "ui-state-hover",
accept: ":not(.ui-sortable-helper)",
drop: function( event, ui ) {
console.log(ui);
$( ui.draggable ).remove();
$( '<div class="favrecentbox"></div>' ).html( ui.draggable.html() ).draggable(draggableOptions).appendTo( this );
}});
Upvotes: 1
Reputation: 4517
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);
}
} );
Upvotes: 0
Reputation: 102
You can try using $(x).css() to manipulate CSS after the drop.....
Upvotes: 1