结合jQuery可拖动,可拖放和可排序 [英] Combining jQuery draggable, droppable, and sortable

查看:97
本文介绍了结合jQuery可拖动,可拖放和可排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试结合jQuery可拖动,可拖放和可排序;但是,我一直有问题.有人可以帮我吗?

I'm trying to combine jQuery draggable, droppable, and sortable; however, I keep having problems. Can anybody help me?

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

Basically, the idea is illustrated here:

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

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

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

This is what I have, but it's very buggy and not pretty:

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>

推荐答案

好的,我能够弄清楚:

Okay, I was able to figure it out: http://codepen.io/anon/pen/jxDCt This actually works well.

这篇关于结合jQuery可拖动,可拖放和可排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆