简体   繁体   English

Jquery UI Droppable:如何根据某些逻辑使用不同的hoverClass值?

[英]Jquery UI Droppable: How can I use different hoverClass values based on some logic?

I am using the JQuery UI droppable library features, and I want to provide visual user feedback when they hover over a droppable target. 我正在使用JQuery UI可放置库功能,我想在将鼠标悬停在可放置目标上时提供可视用户反馈。 For this I can easily use the hoverClass option to specify which class to use when they have a draggable item hovering over. 为此,我可以轻松地使用hoverClass选项来指定当可拖动项目悬停时要使用的类。

But what I want to do is use a different hoverClass value depending on some logic. 但我想要做的是根据某些逻辑使用不同的hoverClass值。 Basically, there are a number of areas that are "droppable", and there is a number of items that can be dragged and dropped - however, not all items can be dropped on all areas. 基本上,有许多区域是“可放置的”,并且有许多项目可以拖放 - 但是,并非所有项目都可以放在所有区域上。 So I would like to have, for example, a green background if the drop is valid, and a red background if the drop is invalid. 例如,如果丢弃有效,我希望有绿色背景,如果丢弃无效,我希望有红色背景。

How can this be done? 如何才能做到这一点? I know what logic I want to use, but where can I add the logic. 我知道我想要使用什么逻辑,但我在哪里可以添加逻辑。 It obviously needs to be somewhere I can access the element being dragged, and the potential drop target element. 它显然需要在某个地方我可以访问被拖动的元素,以及潜在的drop target元素。

My simple code so far is as follows: 到目前为止我的简单代码如下:

$(".DragItem").draggable({
    revert: true,
    helper: "clone"
});

$(".DropItem").droppable({
    tolerance: "touch",
    hoverClass: "DropTargetValid"
});
$(".DropItem").droppable({
    tolerance: "touch",
    hoverClass: "DropTargetValid",
    over: function(event, ui) {
       console.log(ui.draggable); // the draggable object
       console.log($(this)); // the droppable object
    }
});

This should do it. 这应该做到这一点。 On over that event will be triggered on all .DropItem elements. 在此过程中,将在所有.DropItem元素上触发该事件。 You can find more about the available events API here: http://api.jqueryui.com/droppable/ 您可以在此处找到有关可用事件API的更多信息: http//api.jqueryui.com/droppable/

I think your issue is trying to do it with the class itself, when droppable has its own hover event, called over jQuery droppable API #over 我觉得你的问题是试图与类本身,这样做时,可放开有自己的悬停事件,呼吁over jQuery的投掷的API #over

So you'd want: 所以你想要:

$(".DropItem").droppable({
    tolerance: "touch",
    over: function(event, ui) {
        // ... logic goes here
    }
});

The other answers are exactly what I was looking for. 其他答案正是我所寻找的。 However, I want to go into a bit further detail here to give a better example of how to process the logic. 但是,我想在此处进一步详细介绍如何处理逻辑的更好示例。

Lets say for example, with have some simple HTML as follows. 让我们举个例子说,有一些简单的HTML如下。 This HTML basically has 4 draggable objects, and 4 possible drop targets: 这个HTML基本上有4个可拖动的对象,以及4个可能的放置目标:

<div style="margin-bottom:20px;">
    <div data-id="1" class="DragItem">I am 1</div>
    <div data-id="2" class="DragItem">I am 2</div>
    <div data-id="3" class="DragItem">I am 3</div>
    <div data-id="4" class="DragItem">I am 4</div>
</div>
<div>
    <div data-id="123" class="DropItem">I accept 1, 2 and 3</div>
    <div data-id="23" class="DropItem">I accept 2 and 3</div>
    <div data-id="34" class="DropItem">I accept 3 and 4</div>
    <div data-id="1234" class="DropItem">I accept all</div>
</div>

As can be seen, I have used data-* attributes to store specific identifying values. 可以看出,我使用data-*属性来存储特定的标识值。 The IDs on the DragItem identify the drag object, and the IDs on the DropItem contain all valid values. DragItem上的ID标识拖动对象,DropItem上的ID包含所有有效值。

The javascript that processes this logic, and then applies the correct classes is as follows: 处理此逻辑的javascript,然后应用正确的类如下:

$(".DragItem").draggable({
    revert: true,
    helper: "clone"
});

$(".DropItem").droppable({
    tolerance: "touch",
    over: function (event, ui) {
        var dropItem = $(this);
        var dragItem = $(ui.draggable);
        var valid = String(dropItem.data("id")).indexOf(dragItem.data("id")) > -1;
        if (valid) {
            dropItem.addClass("DropTargetValid");
        } else {
            dropItem.addClass("DropTargetInvalid");
        }
    },
    out: function (event, ui) {
        var dropItem = $(this);
        dropItem.removeClass("DropTargetValid");
        dropItem.removeClass("DropTargetInvalid");
    },
    deactivate: function (event, ui) {
        var dropItem = $(this);
        dropItem.removeClass("DropTargetValid");
        dropItem.removeClass("DropTargetInvalid");
    }
});

As can be seen, I am doing a simple "does string contain" logic check. 可以看出,我正在做一个简单的“字符串包含”逻辑检查。 This is fine for small numbers but if there is ever a need to do more than 9 object we would need a more reliable string in the DropItem data-id value. 这适用于小数字,但如果需要执行超过9个对象,我们需要在DropItem data-id值中使用更可靠的字符串。

I also use the out and deactivate events to clear up the applied classes. 我还使用outdeactivate事件来清除应用的类。 In this example I duplicate the code, but this could easily be replaced with a single function used by both events. 在这个例子中,我复制了代码,但是这可以很容易地被两个事件使用的单个函数替换。

Finally, the moment you have all been waiting for, here is a working example. 最后,在你等待的那一刻, 这是一个有效的例子。

Use the Draggable Start/Stop events to add and remove classes to your dropzones. 使用可拖动的开始/停止事件向您的dropzones添加和删除类。

http://api.jqueryui.com/draggable/#event-start http://api.jqueryui.com/draggable/#event-start

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

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