我正在尝试结合jQuery可拖动,可拖放和可排序;但是,我一直有问题。有谁能够帮助我?

基本上,这个想法在这里说明:



A,B,C,D,E磁贴可以在上部可放置对象和可排序对象之间移动。
自定义(*)磁贴只能在左侧的可放置对象和可排序对象之间移动。


另外,我尝试在必要时随时停用和重新激活所有这些功能。



这是我所拥有的,但是它有很多错误且不美观:

JS

var gameContainer = $(".game");

var myTray = $(".tray").sortable({
    containment: gameContainer,
    helper: "clone",
    revert: 100,
    tolerance: "pointer",
    update: function(ev, ui) {
        ui.item.addClass("ontray").css({
            "left": "0px",
            "position": "static",
            "top": "0px"
        });
    }
}).disableSelection();

var setTileDraggable = function(tileSelector) {
    tileSelector.draggable({
        connectToSortable: myTray,
        containment: gameContainer,
        helper: "original",
        revert: "invalid"
    }).disableSelection();
};

var myBoard = $(".board").droppable({
    accept: ".tile:not(.red)",
    drop: function(ev, ui) {
        if (ui.draggable.hasClass("ontray")) {
            // tile (not red) coming from tray, place it into .tiles child div
            var cloneTile = ui.draggable.clone().removeClass("ontray").show();
            myBoard.children(".tiles").append(cloneTile);
            var dropx = ui.offset.left - myBoard.offset().left;
            var dropy = ui.offset.top - myBoard.offset().top;
            cloneTile.css({
                "left": dropx + "px",
                "position": "absolute",
                "top": dropy + "px"
            });
            setTileDraggable(cloneTile);
            ui.helper.remove();
            ui.draggable.remove();
        }
    }
}).disableSelection();

var myCustomTile = $(".custom").droppable({
    accept: ".tile.red",
    drop: function(ev, ui) {
        if (ui.draggable.hasClass("ontray")) {
            // red tile coming from tray
            var cloneTile = ui.draggable.clone().removeClass("ontray").show();
            myCustomTile.append(cloneTile);
            setTileDraggable(cloneTile);
            setTileClick(cloneTile);
            ui.helper.remove();
            ui.draggable.remove();
        } else {
            // red tile staying, move back to original position
            ui.draggable.stop(true, false).animate({
                "left": "0px",
                "top": "0px"
            });
        }
    }
}).disableSelection();

// set up draggables
setTileDraggable(myBoard.children(".tiles").find(".tile"));
setTileDraggable(myCustomTile.find(".tile"));


的HTML

<div class="game">
    <div class="board">
        <div class="tiles">
            <div class="tile">D</div>
            <div class="tile">B</div>
            <div class="tile">E</div>
        </div>
    </div>
    <div class="custom">
        <div class="tile red">X</div>
    </div>
    <div class="tray">
        <div class="tile">C</div>
        <div class="tile">A</div>
    </div>
</div>

最佳答案

如果使所有框都可排序,则这是一个容易得多的项目,因为它们具有现有的相互连接的方法。这是您的示例的快速实现:

Live Demo

$('.alpha').sortable({
  connectWith: '.gamma',
  receive: function (event, ui) {
    if ($(ui.item).hasClass('special')) {
      ui.sender.sortable('cancel');
    }
  }
});

$('.beta').sortable({
  connectWith: '.gamma',
  receive: function (event, ui) {
    if (!$(ui.item).hasClass('special')) {
      ui.sender.sortable('cancel');
    }
  }
});

$('.gamma').sortable({
  appendTo: document.body,
  items: '.tile',
  connectWith: '.alpha, .beta',
  receive: function (event, ui) {
    //console.log(event, ui.item);
    //ui.item.remove(); // remove original item
  }
});


您可以看到它主要是


设置可排序对象,以及
根据您的规范创建自定义接收事件,以接受或拒绝特定种类的图块

关于jquery - 结合jQuery可拖动,可拖放和可排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20385177/

10-09 15:10