简体   繁体   English

jquery draggable / droppable更改快照

[英]jquery draggable / droppable change snap

I am attempting to create a series of drag/droppable divs. 我正在尝试创建一系列拖/拖放div。 Once div1 has been dropped, i will create a new div and have it snap at a new position. 一旦div1被删除,我将创建一个新的div并让它在新位置捕捉。 However the snap position remains the same when div1 has been dropped and div2 is being dragged: ( http://jsfiddle.net/kLpgukkj/2/ ) 但是当div1被删除并且div2被拖动时,捕捉位置保持不变:( http://jsfiddle.net/kLpgukkj/2/

var trackID = "path0";
var trackType = "a";

$('.map-track-box').droppable({
    tolerance: 'touch',
    drop: function(event,ui){
        // RETURNS HOME
        $('#trackDragger').animate({ top: '30px', left: '20px' },500);
    }
});

$('#'+trackID).droppable({
    tolerance: 'touch',
    drop: function(event,ui){
        // IN POSITION
        var pos = $('#'+trackID).position();
        $('#trackDragger').animate({ top: pos.top, left: pos.left },500,function() { 
            $('#draggerBox').append("<div class='"+trackType+"' style='z-index:10;position:absolute;top:"+pos.top+"px;left:"+pos.left+"px;'></div>");
            $('#trackDragger').css({ top: '30px', left: '20px' },500);
            $('#trackDragger').attr('class', 'd');
            $('#'+trackID).removeClass('ui-droppable'); // I attempted to remove the added class, and add it to the new element, but without luck
            if(trackID=="path0") {
                trackID = "path1";
            }else if(trackID=="path1") {
                trackID = "path2";
                trackType = "d";
            }else if(trackID=="path2") {
                trackID = "path3";
                trackType = "e";
            }
            $('#'+trackID).addClass('ui-droppable');
        });
    }
});


$('#trackDragger').draggable({
    revert: 'invalid',
    start: function(event,ui) {
        $('#trackDragger').attr('class', trackType);
    },
    snap: "#"+trackID, // This keeps snapping to initial div (path0) even though trackID is being changed to path1,path2 etc
    snapMode: "inner",
    stop: function(){
        var dragPos = $('#trackDragger').position();
        if(dragPos.left < 101 && dragPos.top < 91) {
            $('#trackDragger').attr('class', 'd');
        }
        //$(this).draggable('option','revert','invalid');
    }
});

The above works well, but the dragged element keeps snapping to path0 (initial draggable div). 上面的效果很好,但拖动的元素一直在捕捉到path0(初始可拖动的div)。 Is there anyway to force a new snap location ? 无论如何强制一个新的快照位置?

Thank you 谢谢

As for the fiddle you've mentioned, you bind the droppable event only to one element. 至于你提到过的小提琴,你只能将droppable事件绑定到一个元素。 That is 那是

$('#'+trackID) // Your variable don't change from "path0" to any other.

As I understand, you want the other black boxes also to have the droppable event bind. 据我所知,你希望其他黑盒也有droppable事件绑定。

Just change the code to the following in the droppable section. 只需将代码更改为droppable部分中的以下内容即可。

$('.path').droppable({});

This would bind the event to every .path element in the page. 这会将事件绑定到页面中的每个.path元素。

see the updated fiddle here . 这里看到更新的小提琴。

I found that adding: 我发现添加:

$('#trackDragger').draggable("option", "snap", "#"+trackID);

will force it to snap to the new element. 将强制它捕捉到新元素。 Furthermore using 再使用

$('#'+trackID).droppable()...

would fail, as it apparently needs to be reinitialized in order to use a different element as droppable. 会失败,因为它显然需要重新初始化才能使用不同的元素作为droppable。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM