简体   繁体   English

如何在jQuery UI中使make.droppable工作?

[英]How can I make.droppable in jQuery UI work?

I designed a simple drag and drop game to train my jQuery UI skills but for some reason I can not drop the circle on the same colored square. 我设计了一个简单的拖放游戏来训练我的jQuery UI技能,但是由于某些原因,我无法将圆圈放到相同颜色的正方形上。 And when it works it goes behind the square instead of being on top :(. What did I do wrong in my code? 并且当它起作用时,它位于正方形后面而不是在顶部:(。我的代码在做什么错?

(In my snippet the circle snaps to the square but in chrome it doesn't) (在我的代码段中,圆圈捕捉到了正方形,但是在chrome中却没有)

Fiddle: https://jsfiddle.net/2udcoyjt/ 小提琴: https : //jsfiddle.net/2udcoyjt/

JS here: JS在这里:

$(".draggers").draggable({
  revert: 'invalid'
});

$(".droppers").droppable({
  accept: function(item) {
    return $(this).data("color") == item.data("color");
  },
  drop: function(event, ui) {
    var $this = $(this);
    ui.draggable.position({
      my: "center",
      at: "center",
      of: $this,
      using: function(pos) {
        $(this).animate(pos, 150, "linear");
      }
    });
  }
});

You must use the ids of the droppers to define the accept and drop options for each, as the droppable the accept function is run on every drag event (start, stop, enter, leave), as well as on drags along the edge of the page. 您必须使用放置控件的ID来定义每个控件的接受和放置选项,因为在每个拖动事件(开始,停止,输入,离开)以及沿着控件边缘的拖动操作上都将运行可放置接受功能页。 Try 尝试

console.log($(this).data.data("color") )

and you will see that you get red blue on every event (start, stop, enter, leave ..) 并且您会看到在每个事件中(开始,停止,进入,离开..)都会显示红色蓝色

Here is a working fiddle 是一个工作的小提琴

暂无
暂无

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

相关问题 如何使用jQuery UI制作可拖动,可排序,可放下的卡片物品? - How can I use jQuery UI to make a draggable, sortable, droppable card thing? 如何绝对定位jQuery可拖动对象,并仍然使其正确移动以使可放置对象正常工作? (仅IE有问题) - How can I absolutely position jQuery draggables and still make them move correctly for droppable to work? (a problem just with IE) 在JQuery UI中,当项目在可放置区域之间拖动时,如何减少计数? - In JQuery UI, how can I decrement the count as items are dragged between droppable areas? 如何在Jquery中的droppable上初始化可拖动对象? - How can I initialize a draggable on a droppable in Jquery? 如何在旧版jQuery UI中使jQuery UI自动完成功能正常工作? - How can I make jQuery UI Autocomplete work in an older version of jQuery UI? Jquery UI Droppable:如何根据某些逻辑使用不同的hoverClass值? - Jquery UI Droppable: How can I use different hoverClass values based on some logic? 如何在droppable over事件中获取可拖动的ui? - How can i get draggable ui in droppable over event? javascript-如何在jQuery'.on'函数中使用jQuery UI .droppable? - How to use jquery ui '.droppable' with jquery '.on' function? 无法禁用jQuery UI中的Droppable - can't disable droppable in jquery ui 如何使用jQuery mobile添加可拖放事件侦听器? - How can I add draggable and droppable event listeners with jQuery mobile?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM