Oto przykład użycia rozwiązania współrzędnych XY.
Working example on jsfiddle
Przykładem może być ulepszona, ale jest dobry punkt wyjścia.
Po prostu śledzi położenie myszy i sprawdza, czy znajdują się wewnątrz obwiedni zrzucanych obiektów. Dlatego jeśli zdarzenie mouseup jest uruchamiane na którymkolwiek z nich, przeciągnięty obiekt jest upuszczany.
Możesz także użyć współrzędnych obiektu, który przeciągasz, aby wykryć, czy znajduje się on na polu, które można zrzucić, ale wymaga nieco więcej kodu do znajdowania obwiedni prostokąta i wystarczy mi mysz.
Kod używa jQuery, ale nie ma jQueryUI. Testowałem w przeglądarce Chrome, Firefox i Opera, ale nie w IE :)
Dodaję tutaj także kod, jeśli jsfiddle nie jest dostępny.
HTML
<p>Drag orange boxes to grey ones</p>
<div class="droppable"></div>
<div class="droppable"></div>
<div class="droppable"></div>
<div class="droppable"></div>
<div class="draggable"></div>
<div class="draggable"></div>
<div class="draggable"></div>
CSS
.droppable {
width:50px;
height:50px;
float: left;
background-color: #DDD;
margin: 5px;
}
.draggable {
width:40px;
height:40px;
float: right;
background-color: #FC0;
margin: 5px;
cursor: pointer;
}
.dropped {
background-color: #FC0;
}
.somethingover {
background-color: #FCD;
}
JS
var dragged, mousex, mousey, coordinates = [];
var continueDragging = function(e) {
// Change the location of the draggable object
dragged.css({
"left": e.pageX - (dragged.width()/2),
"top": e.pageY - (dragged.height()/2)
});
// Check if we hit any boxes
for (var i in coordinates) {
if (mousex >= coordinates[i].left && mousex <= coordinates[i].right) {
if (mousey >= coordinates[i].top && mousey <= coordinates[i].bottom) {
// Yes, the mouse is on a droppable area
// Lets change the background color
coordinates[i].dom.addClass("somethingover");
}
} else {
// Nope, we did not hit any objects yet
coordinates[i].dom.removeClass("somethingover");
}
}
// Keep the last positions of the mouse coord.s
mousex = e.pageX;
mousey = e.pageY;
}
var endDragging = function(e) {
// Remove document event listeners
$(document).unbind("mousemove", continueDragging);
$(document).unbind("mouseup", endDragging);
// Check if we hit any boxes
for (var i in coordinates) {
if (mousex >= coordinates[i].left && mousex <= coordinates[i].right) {
if (mousey >= coordinates[i].top && mousey <= coordinates[i].bottom) {
// Yes, the mouse is on a droppable area
droptarget = coordinates[i].dom;
droptarget.removeClass("somethingover").addClass("dropped");
dragged.hide("fast", function() {
$(this).remove();
});
}
}
}
// Reset variables
mousex = 0;
mousey = 0;
dragged = null;
coordinates = [];
}
var startDragging = function(e) {
// Find coordinates of the droppable bounding boxes
$(".droppable").each(function() {
var lefttop = $(this).offset();
// and save them in a container for later access
coordinates.push({
dom: $(this),
left: lefttop.left,
top: lefttop.top,
right: lefttop.left + $(this).width(),
bottom: lefttop.top + $(this).height()
});
});
// When the mouse down event is received
if (e.type == "mousedown") {
dragged = $(this);
// Change the position of the draggable
dragged.css({
"left": e.pageX - (dragged.width()/2),
"top": e.pageY - (dragged.height()/2),
"position": "absolute"
});
// Bind the events for dragging and stopping
$(document).bind("mousemove", continueDragging);
$(document).bind("mouseup", endDragging);
}
}
// Start the dragging
$(".draggable").bind("mousedown", startDragging);
Czy używasz interfejsu jQuery? – Purag
Nie, używam niestandardowego utworzonego przeciągnięcia – user969724
Pomoże to zobaczyć ten kod. Albo umieść to w pytaniu, albo wklej je w [jsfiddle] (http://jsfiddle.net). – Purag