拖放 2 个放置目标

发布于 2024-10-11 02:04:15 字数 234 浏览 3 评论 0原文

我正在寻找一个拖放示例,其中拖动项目有两个不同的区域,必须与两个可放置区域相匹配。

例子: alt text

我希望蓝色拖动项目恢复,除非将其放置在其每个红色子项着陆的位置一个绿色区域。

理想情况下,我想使用 jquery ui (因为我有使用它的经验),但任何 javascript 库都可以,提前致谢。

I am looking for an example of drag and drop where the drag item has two distinct areas that have to match up with two droppable areas.

Example:
alt text

I would like the blue drag item to revert unless it is dropped in a position where each of its red children land on a green area.

Ideally I would like to use jquery ui (as I have experience with it), but any javascript library would be fine, thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九公里浅绿 2024-10-18 02:04:15

您可以通过使用 draggable/droppable 选项的组合来实现此目的。给定这样的 HTML:(

<div id="blue" class="valid">
    <div id="red-one" class="red"></div>
    <div id="red-two" class="red"></div>
</div>

<div id="green-container">
    <div id="green-one" class="green">
    </div>
    <div id="green-two" class="green">
    </div>
</div>

省略 CSS,我确实添加了一些规则,请参见下面的小提琴)。

你可以这样编写 JavaScript:

function isInside(one, other) {
    return one.offset().left >= other.offset().left &&
        one.offset().top >= other.offset().top &&
        one.offset().top + one.height() <= other.offset().top + other.height() &&
        one.offset().left + one.width() <= other.offset().left + other.width();
}

$("#blue").draggable({
    drag: function(event, ui) {
        var $this = $(this);
        var $reds = $this.children(".red");
        var $greens = $("#green-container").children(".green");
        var firstRed = $reds.first();
        var firstGreen = $greens.first();
        var secondRed = $reds.last();
        var secondGreen = $greens.last();

        if (isInside(firstRed, firstGreen) && isInside(secondRed, secondGreen)) {
            $this.addClass('valid');
        }
        else {
            $this.removeClass('valid');
        }       
    },
    revert: 'invalid'
});


$("#green-container").droppable({ accept: ".valid" });

在这里查看:http://jsfiddle.net/andrewwhitaker/g6FKz/

注释:

  • 出于某种原因,我必须首先将“有效”类应用于“蓝色”div,否则目标可放置元素将不会接受拖动的元素,即使它是有效的(将感谢对此的一些意见)。不确定这是怎么回事,可能是 jQueryUI 中的一个错误。不过没什么大不了的。
  • 目标 droppable 并不完全是两个绿色元素,它是一个包含这些元素的白色 div 。从例子中应该可以清楚地看出这一点。
  • 每次移动可拖动 div 时,都会调用“drag”事件,该事件确定红色框是否位于绿色框内,并为可拖动 div 分配一个 valid 类。代码>div。可放置对象仅接受有效可拖动对象。

希望有帮助!

You can accomplish this by using a combination of draggable/droppable options. Given HTML like this:

<div id="blue" class="valid">
    <div id="red-one" class="red"></div>
    <div id="red-two" class="red"></div>
</div>

<div id="green-container">
    <div id="green-one" class="green">
    </div>
    <div id="green-two" class="green">
    </div>
</div>

(omitting CSS, I did add some rules, see in the fiddle below).

You can write JavaScript like this:

function isInside(one, other) {
    return one.offset().left >= other.offset().left &&
        one.offset().top >= other.offset().top &&
        one.offset().top + one.height() <= other.offset().top + other.height() &&
        one.offset().left + one.width() <= other.offset().left + other.width();
}

$("#blue").draggable({
    drag: function(event, ui) {
        var $this = $(this);
        var $reds = $this.children(".red");
        var $greens = $("#green-container").children(".green");
        var firstRed = $reds.first();
        var firstGreen = $greens.first();
        var secondRed = $reds.last();
        var secondGreen = $greens.last();

        if (isInside(firstRed, firstGreen) && isInside(secondRed, secondGreen)) {
            $this.addClass('valid');
        }
        else {
            $this.removeClass('valid');
        }       
    },
    revert: 'invalid'
});


$("#green-container").droppable({ accept: ".valid" });

Check it out here: http://jsfiddle.net/andrewwhitaker/g6FKz/

Notes:

  • For some reason I had to apply the 'valid' class initially to the 'blue' div, or else the target droppable would not accept the dragged element, even if it was valid (would appreciate some input on this). Not sure what's up with that, might be a bug in jQueryUI. Not a huge deal though.
  • The target droppable isn't exactly the two green elements, it's a white div that contains those elements. This should be clear from the example.
  • Every time you move the draggable div, the "drag" event is called, which determines if the red boxes are inside the green ones and assigns a valid class to the draggable div. The droppable object only accepts valid draggables.

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文