我正在尝试在不使用HTML5 拖放功能的情况下为我的网站创建拖放功能。我已经弄清楚了拖动部分,但我无法将它们拖放到特定的放置区域。这是我的代码:jQuery UI
HTML
<ul id="grid">
<li class="drop">
<div class="drag" id="el_1"><div>
</li>
<li class="drop">
<div class="drag" id="el_2"><div>
</li>
<li class="drop">
<div class="drag" id="el_3"><div>
</li>
<li class="drop"></li>
<li class="drop"></li>
<li class="drop"></li>
</ul>
<script>
$(function() {
$('.handle').mousedown(drag_and_drop);
});
</script>
CSS
#grid{
position:relative;
width:360px;
height:auto;
}
.drop{
width:100px;
height:100px;
margin:10px;
}
.drag{
position:absolute;
width:100px;
height:100px;
}
#el_1{ top:10px; left:10px}
#el_2{ top:10px; left:130px}
#el_3{ top:10px; left:250px}
脚本
function drag_and_drop(e){
var drag = $(this);
var drop = drag.parent();
drag.addClass('draggable');
drag_h = drag.outerHeight(),
drag_w = drag.outerWidth(),
pos_y = drag.offset().top + drag_h - e.pageY,
pos_x = drag.offset().left + drag_w - e.pageX;
$('.draggable').css('z-index', 9000).mousemove(function(e) {
/*
IN THIS MOUSEMOVE FUNCTION IS THERE ANY WAY TO DETECT IF THE DRAGGABLE DIV IS "HOVERING" OVER THE li.drop ELEMENTS?
*/
$('.draggable').offset({
top: e.pageY + pos_y - drag_h,
left: e.pageX + pos_x - drag_w
});
}).mouseup(function() {
drag.removeClass('draggable');
});
e.preventDefault(); // stops the browser default drag method
};
有没有什么方法可以让我开始拖动一个对象,它可以检测到它是否在一个特定的 li.drop 上——我可以从中将它放入?