0

我是一个完整的 jQuery 和 Javascript 菜鸟,试图让一个非常简单的拖放现场演示工作。我越来越近了,但我拖动的元素被赋予了绝对定位,所以它们不能很好地融合在一起。

标记非常简单:

<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> 

还有我伤心的小 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'));

我已经进行了拖动工作,但是当它们放下时,它们会保留绝对定位(设置了左侧、顶部等)。我该如何关闭它?

4

3 回答 3

1

您可以尝试使用 $(x).css() 在 drop 后操作 CSS .....

于 2011-10-25T20:20:01.267 回答
1

没有必要让它比现在更复杂。删除 CSS 似乎是个坏主意。

见: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 );
    }});
于 2011-10-25T20:51:39.597 回答
0

您可以在放置时将其作为子项添加到可放置容器中。

$('.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);
            }
    } );
于 2011-10-25T20:36:38.547 回答