简体   繁体   中英

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. 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)

Fiddle: https://jsfiddle.net/2udcoyjt/

JS here:

$(".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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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