简体   繁体   English

jQuery UI可拖动和可拖放仅返回原始位置

[英]jQuery UI Draggable and Droppable Return to Original Position Only

Codepen here: https://codepen.io/anon/pen/weZwVx 此处的Codepen: https ://codepen.io/anon/pen/weZwVx

I have some images that are draggable. 我有一些可拖动的图像。 I need them to be returned to their original position only . 我需要他们返回到他们唯一的原始位置。 So essentially even if they are dragged back to the wrong spot they will snap to their original position in the table. 因此,基本上,即使将它们拖回到错误的位置,它们也将对齐到表格中的原始位置。

Right now when you drag it back to the original position it can be added to another td even it contains another image. 现在,当您将其拖回原始位置时,即使它包含另一个图像,也可以将其添加到另一个td中。 The image should be able to be dropped in the purple box and then if someone moves it back to the black table it should go back to it's original position only, even if it is dragged back to another spot. 图像应该可以放在紫色框中,然后,如果有人将其移回黑色桌子,则即使将其拖回到另一个位置,也应仅回到其原始位置。

Here's the JS: 这是JS:

$( ".emojis" ).draggable({
                revert : function(event, ui) {
        $(this).data("ui-draggable").originalPosition = {
            top : 0,
            left : 0
        };
        return !event;
    }
});
            $('#drop-em').droppable({
                drop: function(ev, ui) {
                    $(ui.draggable).detach().css({top: 0,left: 0}).appendTo(this);
                }

            });
            $('#emoji-table td').droppable({
                drop: function(ev, ui) {


                    $(ui.draggable).detach().css({top: 0,left: 0}).prependTo(this);

                }

            });

I think the easiest way based on the code you provided would be to set a matching id to the and elements, that way when the image is being dropped, it can check to see if the id's match each other. 我认为,基于您提供的代码的最简单方法是为和元素设置匹配的ID,这样在删除图像时,它可以检查ID是否彼此匹配。 If they don't match, you just send the image to the correct element. 如果它们不匹配,则只需将图像发送到正确的元素。

  $( ".emojis" ).draggable({
    revert : function(event, ui) {
    // on older version of jQuery use "draggable"
    // $(this).data("draggable")
    // on 2.x versions of jQuery use "ui-draggable"
    // $(this).data("ui-draggable")
    var bRevertingImg = this.parent("#drop-em");

    //Only apply logic when reverted
    //needed since this function is called anytime .emoji is dropped
    if (!bRevertingImg.length) {
      var imgID = $(this).data("id");
      var cellID = $(this).parent().data("id");

      //If the child and parent ids don't match, we move the child to the correct parent
      if (imgID !== cellID) {
        var correctCell = $("#emoji-table").find("td[data-id='" + imgID + "']");
        correctCell.prepend(this)        
      }

      // return boolean
    }
    return !event;   

    // that evaluate like this:
    // return event !== false ? false : true;
  }
});

I edited your codepen to show it in action here 我编辑了您的代码笔以在此处显示其效果

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

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