jQuery draggable - 只记住第一次拖动

时间:2017-03-01 12:51:14

标签: javascript jquery jquery-ui draggable jquery-ui-draggable

我有2件物品。当您将项目拖动到颜色上时,它应该提醒并告诉您所选择的项目以及您拖动的颜色。

颜色警报工作正常,但它只会记住您拖动的第一个项目 - 直到您刷新页面并选择另一个。

我创建了两个单独的可拖动实例但仍然出现问题。我无法用爱情或金钱来解决这个问题!

这是一个JSFIDDLE:https://jsfiddle.net/ytcqsch6/

HTML

    <div id="color-block-1" class="color-blocks" theColor="blue"></div>
    <div id="color-block-2" class="color-blocks" theColor="purple"></div>
    <div id="color-block-3" class="color-blocks" theColor="pink"></div>


    <div id="choose-box">
        <h1 id="choose-box-header">Title</h1>
        <div id="drag-box">   
            <div class="drag drag-eg" >ELECTRICAL<br>GOODS</div>
            <div class="drag drag-fa" >FASHION</div>
        </div>
    </div>

<script
  src="https://code.jquery.com/jquery-2.2.4.min.js"
  integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
  crossorigin="anonymous"></script>
<script
  src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"
  integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU="
  crossorigin="anonymous"></script>

JS:

/// DRAGGABLE ///
$.fn.liveDroppable = function (opts) {
        if (!$(this).data("ctDropInit")) {
            $(this).data("ctDropInit", true).droppable(opts);
        }
};

$(".drag-eg").each(function(index, elem) {
    $(elem).draggable({
        cursor: "grab",
        distance: 0,
        opacity: 1,
        helper: 'clone',
        start: function() {
            $(this).css("opacity","0");
            startDroppable("Electrical Goods");
        },
        stop: function() {
            $(this).css("opacity","1");
        }
    });
});

$(".drag-fa").each(function(index, elem) {
    $(elem).draggable({
        cursor: "grab",
        distance: 0,
        opacity: 1,
        helper: 'clone',
        start: function() {
            $(this).css("opacity","0");
            startDroppable("Fashion");
        },
        stop: function() {
            $(this).css("opacity","1");
        }
    });
});


function startDroppable($industry){
    $('.color-blocks').liveDroppable({
        hoverClass: "ctDropHover",
        drop: function (event, ui) {
            alert( "Dropped! - " + $industry + "----" + $(this).attr("theColor"));
    }
});
}
/// DRAGGABLE END ///

1 个答案:

答案 0 :(得分:2)

您可以在js代码中尝试以下操作。

/// DRAGGABLE ///
$.fn.liveDroppable = function (opts) {
        if (!$(this).data("ctDropInit")) {
            $(this).data("ctDropInit", true).droppable(opts);
        }
};

$(".drag-eg").each(function(index, elem) {

    $(elem).draggable({
        cursor: "grab",
        distance: 0,
        opacity: 1,
        helper: 'clone',
        start: function() {
            $(this).css("opacity","0");
            startDroppable("Electrical Goods");
        },
        stop: function() {
            $(this).css("opacity","1");
        }
    });
});

$(".drag-fa").each(function(index, elem) {
    $(elem).draggable({
        cursor: "grab",
        distance: 0,
        opacity: 1,
        helper: 'clone',
        start: function() {
            $(this).css("opacity","0");
            startDroppable("Fashion");
        },
        stop: function() {
            $(this).css("opacity","1");
        }
    });
});


function startDroppable($industry){
    $('.color-blocks').liveDroppable({
        hoverClass: "ctDropHover",
        drop: function (event, ui) {
            alert( "Dropped! - " + $(ui.draggable[0]).text() + "----" + $(this).attr("theColor"));  //only changed here..

    }
});
}
/// DRAGGABLE END ///