拖曳排序、浮動提示、碰撞偵測這類互動,都會遇到「兩個方塊有沒有壓到彼此」的問題。原理其實很單純:只要判斷一個元素的四個角,是否有任何一個落在另一個元素的範圍內,就能得出結果。
$("#result").text(
isOverlap("one","two") + "," +
isOverlap("one","three") + "," +
isOverlap("two","three")
);
function isOverlap(idOne, idTwo){
var objOne = $("#" + idOne),
objTwo = $("#" + idTwo),
offsetOne = objOne.offset(),
offsetTwo = objTwo.offset(),
topOne = offsetOne.top, topTwo = offsetTwo.top,
leftOne = offsetOne.left, leftTwo = offsetTwo.left,
widthOne = objOne.width(), widthTwo = objTwo.width(),
heightOne = objOne.height(), heightTwo = objTwo.height();
// 判斷 objTwo 的四個角是否落在 objOne 的範圍內
var leftTop = leftTwo > leftOne && leftTwo < leftOne + widthOne
&& topTwo > topOne && topTwo < topOne + heightOne,
rightTop = leftTwo + widthTwo > leftOne && leftTwo + widthTwo < leftOne + widthOne
&& topTwo > topOne && topTwo < topOne + heightOne,
leftBottom = leftTwo > leftOne && leftTwo < leftOne + widthOne
&& topTwo + heightTwo > topOne && topTwo + heightTwo < topOne + heightOne,
rightBottom = leftTwo + widthTwo > leftOne && leftTwo + widthTwo < leftOne + widthOne
&& topTwo + heightTwo > topOne && topTwo + heightTwo < topOne + heightOne;
return leftTop || rightTop || leftBottom || rightBottom;
}
四個變數分別代表:左上角、右上角、左下角、右下角。任一為真即代表重疊。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>JSCode demo</title>
<style type="text/css">
div { width:200px; height:200px; background:#EEE; }
#two { position:absolute; left:100px; top:50px; background:#F60; }
</style>
<script src="/js_lib/jQuery-1.7.2.js" type="text/javascript"></script>
</head>
<body>
<div id="one">One</div>
<div id="two">Two</div>
<div id="three">Three</div>
<h1 id="result"></h1>>
<script type="text/javascript">
$("#result").text(
isOverlap("one","two") + "," +
isOverlap("one","three") + "," +
isOverlap("two","three")
);
function isOverlap(idOne, idTwo){
var objOne = $("#" + idOne),
objTwo = $("#" + idTwo),
offsetOne = objOne.offset(),
offsetTwo = objTwo.offset(),
topOne = offsetOne.top, topTwo = offsetTwo.top,
leftOne = offsetOne.left, leftTwo = offsetTwo.left,
widthOne = objOne.width(), widthTwo = objTwo.width(),
heightOne = objOne.height(), heightTwo = objTwo.height();
var leftTop = leftTwo > leftOne && leftTwo < leftOne + widthOne
&& topTwo > topOne && topTwo < topOne + heightOne,
rightTop = leftTwo + widthTwo > leftOne && leftTwo + widthTwo < leftOne + widthOne
&& topTwo > topOne && topTwo < topOne + heightOne,
leftBottom = leftTwo > leftOne && leftTwo < leftOne + widthOne
&& topTwo + heightTwo > topOne && topTwo + heightTwo < topOne + heightOne,
rightBottom = leftTwo + widthTwo > leftOne && leftTwo + widthTwo < leftOne + widthOne
&& topTwo + heightTwo > topOne && topTwo + heightTwo < topOne + heightOne;
return leftTop || rightTop || leftBottom || rightBottom;
}
</script>
</body>
</html>
「四角判斷法」夠直觀,但有兩個邊界情況會誤判:
isOverlap(A,B) || isOverlap(B,A))即可。>、<),因此「邊緣正好相切」會被視為沒有重疊,這通常是想要行為,但要在規格上講清楚。如果只要單純判斷矩形相交,其實有更簡潔的寫法:只要 A.right < B.left || A.left > B.right || A.bottom < B.top || A.top > B.bottom 四個條件都不成立,即為重疊。這種寫法天生就能處理包含關係,程式碼也短得多,值得優先考慮。