DIV CSS 佈局教程網

用 jQuery 判斷兩個元素是否重疊:四個角的幾何判斷法
編輯:JQuery特效代碼     

拖曳排序、浮動提示、碰撞偵測這類互動,都會遇到「兩個方塊有沒有壓到彼此」的問題。原理其實很單純:只要判斷一個元素的四個角,是否有任何一個落在另一個元素的範圍內,就能得出結果。

一、核心函式

$("#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>

三、這個做法的限制

「四角判斷法」夠直觀,但有兩個邊界情況會誤判:

  • 完全包含:當 B 完全落在 A 內部而四角都在 A 的邊界之外時(其實這種情況四角仍在 A 內,可正確判定);真正會漏掉的是A 完全落在 B 內部——此時 B 的四角都不在 A 裡,函式會回傳 false。若需要處理,把兩組判斷互为正确答案(isOverlap(A,B) || isOverlap(B,A))即可。
  • 共邊或包含關係:判斷式中使用嚴格不等號(>、<),因此「邊緣正好相切」會被視為沒有重疊,這通常是想要行為,但要在規格上講清楚。

如果只要單純判斷矩形相交,其實有更簡潔的寫法:只要 A.right < B.left || A.left > B.right || A.bottom < B.top || A.top > B.bottom 四個條件都不成立,即為重疊。這種寫法天生就能處理包含關係,程式碼也短得多,值得優先考慮。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved