Frage

As described, i want to be able to drag some divs from a sort of menu that i have, and be able to drop them to a specific div.. i managed to do sort of it following this..

http://jsfiddle.net/bysnc/

however, i want to add a "x" button on each one, so that onclick of the "x" button the draggable/droppable element to return (revert) to its original position..

i had found this

http://jsfiddle.net/v7n6w/

which seemed kind of what i wanted (actually i want a unique button for each and not a overall one.. but 1) this doesn't seem to be dropped even on jsfiddle 2) on my test, i get an error in the console "Uncaught TypeError: Cannot read property 'originalPosition' of undefined "

and specifically the error gets in this line ui.draggable.data("draggable").originalPosition);

here is the js part..

function revertDraggable($selector) {
    $selector.each(function() {
        var $this = $(this),
            position = $this.data("originalPosition");

        if (position) {
            $this.animate({
                left: position.left,
                top: position.top
            }, 500, function() {
                $this.data("originalPosition", null);
            });
        }
    });
}   

$(document).ready(function() {
    $("#drag").draggable({
        revert: "invalid"
    });

    $("#floor").droppable({
        drop: function(event, ui) {
            if (!ui.draggable.data("originalPosition")) {
                ui.draggable.data("originalPosition",
                    ui.draggable.data("draggable").originalPosition);
            }
        }
    });

    $("#other").click(function() {
        revertDraggable($("#drag"));
    });

});

and my html looks like this

droppable position i want to be able to be dropped only in the foo

<div id="container">
    <div id="floor">foo</div>
    <div id="other">bar</div>
</div>

and the initial position

 <div class="menu" id="power" width="300">
<div class="options ui-widget-content" id="drag"><img src="1.jpg"/></div>
    <div class="options ui-widget-content" id="drag2"><img src="2.jpg"/></div>
</div>

p.s. can this be down with just jquery and not plugin jquery.ui?

War es hilfreich?

Lösung

Mixed code from 2 samples

    $('#floor').droppable({
    tolerance: 'fit'
});

$('.draggable-div').draggable({
    revert: 'invalid',
    stop: function(){
        $(this).draggable('option','revert','invalid');
        $(this).find('.undo').show();
    }
});

$('.draggable-div').find('.undo').click(function(i, e) {
    var $div = $(this).parent();
    revertDraggable($div);
});

$('#floor').droppable({
    greedy: true,
    tolerance: 'touch',
    drop: function(event,ui){
       // ui.draggable.draggable('option','revert',true);
        if (!ui.draggable.data("originalPosition")) {
        ui.draggable.data("originalPosition",
                          ui.draggable.data("draggable").originalPosition);
        
        }


        $(this).find('.undo').show();
    }
});

function revertDraggable($selector) {
    $selector.each(function() {
        var $this = $(this),
            position = $this.data("originalPosition");

        if (position) {
            $this.animate({
                left: position.left,
                top: position.top
            }, 500, function() {
                $this.data("originalPosition", null);
            });
        }
    });
    
    $selector.find('.undo').hide();
}

See full code here.

It works now

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top