Вы даже можете продвинуть идею Кбосака немного дальше:
var req = function () {
...
$(".draggable").draggable({
containment: '#container',
scroll: false,
stop: req
});
Другими словами, создайте перетаскиваемый объект, который вызывает функцию «req», когда перетаскивание останавливается.
Кроме того, вы также можете полностью переписать это в более стандартной форме:
function req () {
...
и это будет точно так же. Также вы можете сделать:
$(function() {
вместо:
$(document).ready(function() {
и вы можете объединить два перетаскиваемых вызова. Итак ... если бы я писал это, окончательный код был бы:
function req () {
...*insert code for req here*...
};
$(function() {
$(".draggable").draggable({
containment: '#container',
scroll: false,
stack: { group: '#container', min: 1 },
stop: req
});
$("*", document.body).click(function (e) {
var offset = $(this).offset();// get the offsets of the selected div
e.stopPropagation();
var theId = $(this).attr('id');// get the id of the selceted div
$("#result").text(this.tagName + " id=" + theId + " (" + offset.left + "," + offset.top +")");
//post x,y to php (and the id of the elemnt)
$.post("http://localhost/index.php", "id=" + theId + "&x=" + offset.left + "&y=" + offset.top);
});
req();
});