在droppable

时间:2016-10-12 16:31:16

标签: jquery html css

我正在处理一个拖放项目,并遇到了一个小问题。 我想在dropraable上删除draggable之后禁用它。我有一个函数disableDrag,但我在控制台中出现错误,然后它会记录,但不会执行该功能。 有谁知道为什么?还有更好的方法吗?

HTML设置:

<div class="container">

  <div id="key" class="fragment">
    <div class="key"></div>
  </div>

  <div id="dragonkey" class="fragment">
    <div class="key"></div>
  </div>

  <div id="inventory">
    <div class="slot" id="slot1"></div>
    <div class="slot" id="slot2"></div>
  </div>

</div>

CSS设置:

.container{
  width:300px;
  height:300px;
  background-color:#ccc;
}
#key, #dragonkey{
  width:20px;
  height:20px;
  position:absolute !important;
  z-index:999;
  cursor:pointer;
  background-color:DarkGoldenRod;
  transform:rotate(45deg);
    -webkit-transform:rotate(45deg);
    -moz-transform:rotate(45deg);
    -o-transform:rotate(45deg);
}
#key{
  top:5%;
  left:5%;
}
#dragonkey{
  top:15%;
  left:5%;
}
#inventory{
  width:68px;
  height:68px;
  position:relative;
  float:right;
  margin:5% 5% 0 0;
  transform:rotate(45deg);
    -webkit-transform:rotate(45deg);
    -moz-transform:rotate(45deg);
    -o-transform:rotate(45deg);
}
.slot{
  border:2px solid #fff;
  width:30px;
  height:30px;
  float:left;
}
#slot1, #slot2{
  background-color:rgba(0,0,0,1);
}

.ui-droppable-active{
  background-color:rgba(184,134,11,0.7) !important;
}

这是jQuery设置:

// Javascript document

$(document).ready(function() {

  $("#key").draggable({
    containment: ".container"
  });

  $("#dragonkey").draggable({
    containment: ".container"
  });

  $("#slot1").droppable({
    accept: "#key",
    drop: dropAnimate
  });

  $("#slot2").droppable({
    accept: "#dragonkey",
    drop: dropAnimate
  });

  function dropAnimate(event, ui) {

    console.log( $(ui.draggable).attr('id'));

    var $this = $(this);

    var width = $this.width();
    var height = $this.height();
    var cntrLeft = (width / 2) - (ui.draggable.width() / 2);
    var cntrTop = (height / 2) - (ui.draggable.height() / 2);

    ui.draggable.position({
      my: "center",
      at: "center",
      of: $this,
      using: function(pos) {
        $(this).animate(pos, "slow", "linear");
      }
    });
    disableDrag();
  }

  function disableDrag () {
    $(this).draggable({
      disabled: true
    });
    console.log("DISABLED");
  }


}); <!-- END OF DOCUMENT READY -->

2 个答案:

答案 0 :(得分:6)

您可以使用ui.draggable元素设置disabled: true

ui.draggable.draggable({disabled: true});

检查此示例:

&#13;
&#13;
// Javascript document
$(document).ready(function() {

  $("#key").draggable({
    containment: ".container"
  });

  $("#dragonkey").draggable({
    containment: ".container"
  });

  $("#slot1").droppable({
    accept: "#key",
    drop: dropAnimate
  });

  $("#slot2").droppable({
    accept: "#dragonkey",
    drop: dropAnimate
  });

  function dropAnimate(event, ui) {

    console.log( $(ui.draggable).attr('id'));

    var $this = $(this);

    var width = $this.width();
    var height = $this.height();
    var cntrLeft = (width / 2) - (ui.draggable.width() / 2);
    var cntrTop = (height / 2) - (ui.draggable.height() / 2);

    ui.draggable.position({
      my: "center",
      at: "center",
      of: $this,
      using: function(pos) {
        $(this).animate(pos, "slow", "linear");
      }
    });
    
    ui.draggable.draggable({disabled: true});
  }

}); <!-- END OF DOCUMENT READY -->
&#13;
.container{
  width:300px;
  height:300px;
  background-color:#ccc;
}
#key, #dragonkey{
  width:20px;
  height:20px;
  position:absolute !important;
  z-index:999;
  cursor:pointer;
  background-color:DarkGoldenRod;
  transform:rotate(45deg);
    -webkit-transform:rotate(45deg);
    -moz-transform:rotate(45deg);
    -o-transform:rotate(45deg);
}
#key{
  top:5%;
  left:5%;
}
#dragonkey{
  top:15%;
  left:5%;
}
#inventory{
  width:68px;
  height:68px;
  position:relative;
  float:right;
  margin:5% 5% 0 0;
  transform:rotate(45deg);
    -webkit-transform:rotate(45deg);
    -moz-transform:rotate(45deg);
    -o-transform:rotate(45deg);
}
.slot{
  border:2px solid #fff;
  width:30px;
  height:30px;
  float:left;
}
#slot1, #slot2{
  background-color:rgba(0,0,0,1);
}

.ui-droppable-active{
  background-color:rgba(184,134,11,0.7) !important;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>

<div class="container">

  <div id="key" class="fragment">
    <div class="key"></div>
  </div>

  <div id="dragonkey" class="fragment">
    <div class="key"></div>
  </div>

  <div id="inventory">
    <div class="slot" id="slot1"></div>
    <div class="slot" id="slot2"></div>
  </div>

</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

disableDrag函数中,this并未指向被拖动的元素;它指向Window对象。通过将可拖动元素传递给具有disableDrag($this);的函数来解决此问题。然后在函数function disableDrag(item)中添加一个参数,并在该函数中将$(this).draggable({disabled: true})更改为$(item).draggable("option", "disabled", true);

<强> jsFiddle example