当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 基于jQuery判断两个元素是否有重叠部分的代码

基于jQuery判断两个元素是否有重叠部分的代码

2018年06月15日  | 移动技术网IT编程  | 我要评论

核心代码:

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


原理很简单,就是判断一个元素的四个点是否在另一个元素内部。
完整演示代码:

代码如下:


JSCode
LoginResult Script HTML ALL Edit Share
$("#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;
}
<p id="one">One</p>
<p id="two">Two</p>
<p id="three">Three</p>
<h1 id="result"></h1>
p{
width:200px;
height:200px;
background:#EEE;
}
#two{
position:absolute;
left:100px;
top:50px;
background:#F60;
}
DownLoad
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>JSCode demo</title>
<style type="text/css">
p{
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>
<p id="one">One</p>
<p id="two">Two</p>
<p id="three">Three</p>
<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>
Share link
Embed on your page
Share on Sina
Share on QQ

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网