這個範例與前兩個不同,重點在於把元素拖拽到指定區域內。邏輯方式其實差不多,差別只在於找到目標元素後,改用 appendTo 方法將元素插入其中。
具體代碼如下,有興趣者可以試試看。(由於是初學,做得比較簡單,若還有其他更好的做法,歡迎指點,不勝感激。)
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>測試的拖拽功能</title>
<style type="text/css">
body, div { margin: 0; padding: 0; font-size: 12px; }
.box { position: static; float: left; width: 200px; margin-top: 15px; }
.bor { position: static; width: 100px; height: 100px; margin: 0 0 5px 0; border: 1px solid #ccc; background: #ececec; }
.borp { position: absolute; width: 100px; height: 80px; margin: 10px; padding: 10px; border: 1px solid #ccc; background: #ececec; }
.bg { float:left; width: 100px; height: 100px; margin: 8px 5px 0 auto; padding: 10px; border: 1px solid #ccc; }
.bgColor { border: 1px dashed #f00; }
</style>
<script type="text/javascript" src="jquery-1.6.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var move = false; //移動的初始化
var $bg = $(".bg");
var initDiv, tarDiv, tarDivHalf = 0, wHalf = 0; //拖拽物件、目標物件
var relPos = {x: 0, y: 0};
var dragPos = {x1: 0, x2: 0, y1: 0, y2: 0};//拖拽物件的四個坐標
var tarDivPos = {x1: 0, y1: 0, x2: 0, y2: 0}; //目標物件的四個坐標
$(".bor").each(function() {
$(this).mousedown(function(event) {
borSub = $(this).index();
initDiv = $(".bor").eq(borSub); //拖拽物件
relPos.x = event.pageX - $(this).offset().left;
relPos.y = event.pageY - $(this).offset().top;
move = true;
});
$(document).mousemove(function(event) {
if (!move) { return false; }
initDiv.removeClass("bor").addClass("borp");
dragPos.x1 = event.pageX - relPos.x;
dragPos.y1 = event.pageY - relPos.y;
dragPos.x2 = dragPos.x1 + initDiv.innerWidth();
dragPos.y2 = dragPos.y1 + initDiv.innerHeight();
initDiv.css({ left: dragPos.x1 +'px', top: dragPos.y1 + 'px' });
$bg.each(function() {
tarDiv = $(this);
tarDivPos.x1 = tarDiv.offset().left;
tarDivPos.x2 = tarDivPos.x1 + tarDiv.width();
tarDivPos.y1 = tarDiv.offset().top;
tarDivPos.y2 = tarDivPos.y1 + tarDiv.height();
tarDivHalf = tarDiv.height()/2;
wHalf = tarDiv.width()/2;
if (dragPos.x2 >= tarDivPos.x1 + wHalf && dragPos.x2 <= tarDivPos.x2 + wHalf && dragPos.y2 >= tarDivPos.y1 + tarDivHalf && dragPos.y2 <= tarDivPos.y2 + tarDivHalf ) {
if(tarDiv.children().length >0 ) {return false;}//避免重疊元素移入同一目標
tarDiv.removeClass("bg").addClass("bg bgColor");
} else {
tarDiv.removeClass("bgColor");
}
});
}).mouseup (function(event) {
initDiv.appendTo($(".bgColor"));//利用appendTo將拖動元素加入指定區域
initDiv.removeClass("borp").addClass("bor").removeAttr("style"); //恢復初始樣式
move = false;
});
});
});
</script>
</head>
<body>
<div class="box">
<div class="bor">bor1</div>
<div class="bor">bor2</div>
<div class="bor">bor3</div>
</div>
<div class="box2">
<div class="bg" id="bg1"></div>
<div class="bg" id="bg2"></div>
<div class="bg" id="bg3"></div>
<!-- ...更多 bg 目標區域... -->
</div>
</body>
</html>
原理說明:在 mousedown 時記錄拖拽元素與滑鼠偏移;mousemove 時讓元素隨滑鼠移動,並比對其右下坐標是否落入某個目標區域(以目標區域中心做容差判斷),若命中則為該區域加上高亮樣式 bgColor,同時避免將元素疊到已有子元素的區域;最後在 mouseup 時以 appendTo 將元素插入高亮區域,並還原樣式,即完成拖拽入區的效果。