Code _jquery to determine whether two elements have overlapping portions based on jquery

Source: Internet
Author: User
Core code:
Copy Code code as follows:

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

The simple principle is to determine whether four points of an element are within another element.
Full Demo Code:
Copy Code code as follows:

Jscode
Loginresult JavaScript HTML CSS 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;
}
<div id= "One" >One</div>
<div id= "Two" >Two</div>
<div id= "three" >Three</div>
&LT;H1 id= "Result" >div{
width:200px;
height:200px;
Background: #EEE;
}
#two {
Position:absolute;
left:100px;
top:50px;
Background: #F60;
}
DownLoad
<! DOCTYPE html>
<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>
<body>
<div id= "One" >One</div>
<div id= "Two" >Two</div>
<div id= "three" >Three</div>
&LT;H1 id= "Result" ><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>
Share Link
Embed on your Page
Share on Sina
Share on QQ

Author: artwl

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.