//<![CDATA[
/* collider.js - written by Jason Raymond Buckley */
$(function(){
$.fn.extend({
allCollided:function(jQueryNode, includeBorders, includeMargins){
var r = true, im = !!includeMargins;
this.each(function(i, e){
var o = $(e), ow = o.width(), oh = o.height(), os = o.offset(), ot = os.top, ob = oh+ot, ol = os.left, or = ow+ol;
var oT = parseFloat(o.css('borderTopWidth')), oR = parseFloat(o.css('borderRightWidth')), oB = parseFloat(o.css('borderBottomWidth'));
var oL = parseFloat(o.css('borderLeftWidth'));
if(includeBorders){
if(im){
oB += parseFloat(o.css('marginBottom'))+oT; oT += parseFloat(o.css('marginTop')); oR += parseFloat(o.css('marginRight'))+oL;
oL += parseFloat(o.css('marginLeft')); ot -= oT; or += oR; ob += oB; ol -= oL;
}
else{
or += oR+oL; ob += oB+oT;
}
}
else{
ot += oT; or += oR; ob += oB; ol += oL;
}
jQueryNode.each(function(i, e){
var t = $(e), tw = t.width(), th = t.height(), ts = t.offset(), tt = ts.top, tb = th+tt, tl = ts.left, tr = tw+tl;
var tT = parseFloat(t.css('borderTopWidth')), tR = parseFloat(t.css('borderRightWidth')), tB = parseFloat(t.css('borderBottomWidth'));
var tL = parseFloat(t.css('borderLeftWidth'));
if(includeBorders){
if(im){
tB += parseFloat(t.css('marginBottom'))+tT; tT += parseFloat(t.css('marginTop')); tR += parseFloat(t.css('marginRight'))+tL;
tL += parseFloat(t.css('marginLeft')); tt -= tT; tr += tR; tb += tB; tl -= tL;
}
else{
tr += tR+tL; tb += tB+tT;
}
}
else{
tt += tT; tr += tR+tL; tb += tB; tl += tL;
}
if(!((ot <= tb && ob >= tt || tt <= ob && tb >= ot) && (ol <= tr && or >= tl || tl <= or && tr >= ol))){
r = false;
return false;
}
});
if(r === false){
return false;
}
});
return r;
},
anyCollided:function(jQueryNode, includeBorders, includeMargins){
var r = false, im = !!includeMargins;
this.each(function(i, e){
var o = $(e), ow = o.width(), oh = o.height(), os = o.offset(), ot = os.top, ob = oh+ot, ol = os.left, or = ow+ol;
var oT = parseFloat(o.css('borderTopWidth')), oR = parseFloat(o.css('borderRightWidth')), oB = parseFloat(o.css('borderBottomWidth'));
var oL = parseFloat(o.css('borderLeftWidth'));
if(includeBorders){
if(includeMargins){
oB += parseFloat(o.css('marginBottom'))+oT; oT += parseFloat(o.css('marginTop')); oR += parseFloat(o.css('marginRight'))+oL;
oL += parseFloat(o.css('marginLeft')); ot -= oT; or += oR; ob += oB; ol -= oL;
}
else{
or += oR+oL; ob += oB+oT;
}
}
else{
ot += oT; or += oR; ob += oB; ol += oL;
}
jQueryNode.each(function(i, e){
var t = $(e), tw = t.width(), th = t.height(), ts = t.offset(), tt = ts.top, tb = th+tt, tl = ts.left, tr = tw+tl;
var tT = parseFloat(t.css('borderTopWidth')), tR = parseFloat(t.css('borderRightWidth')), tB = parseFloat(t.css('borderBottomWidth'));
var tL = parseFloat(t.css('borderLeftWidth'));
if(includeBorders){
if(includeMargins){
tB += parseFloat(t.css('marginBottom'))+tT; tT += parseFloat(t.css('marginTop')); tR += parseFloat(t.css('marginRight'))+tL;
tL += parseFloat(t.css('marginLeft')); tt -= tT; tr += tR; tb += tB; tl -= tL;
}
else{
tr += tR+tL; tb += tB+tT;
}
tw = t.outerWidth(im); th = t.outerHeight(im);
}
else{
tt += tT; tr += tR+tL; tb += tB; tl += tL;
}
if((ot <= tb && ob >= tt || tt <= ob && tb >= ot) && (ol <= tr && or >= tl || tl <= or && tr >= ol)){
r = true;
return false;
}
});
if(r === true){
return false;
}
});
return r;
}
});
/* collider.js - written by Jason Raymond Buckley */
var fixedL = $('#fixedL'), fixedR = $('#fixedR'), fixedM = $('#fixedM'), moving = $('.moving');
if(moving.anyCollided(fixedL, true)){
fixedL.css('color', 'white');
}
$(window).scroll(function(){
if(moving.anyCollided(fixedR)){
fixedR.css('color', 'orange');
}
else{
fixedR.css('color', '#000');
}
if(fixedM.anyCollided(moving, true, true)){
fixedM.css('color', '#0a0');
}
else{
fixedM.css('color', '#000');
}
if($('#red').allCollided(fixedL, true)){
fixedL.css('color', 'red');
}
else if($('#tiny_w_margins').allCollided(fixedL, true, true)){
fixedL.css('color', 'yellow');
}
else if($('#borders_only').allCollided(fixedL, true)){
fixedL.css('color', 'lightBlue');
}
else if($('#green').allCollided(fixedL)){
fixedL.css('color', 'lightGreen');
}
else if(fixedL.allCollided($('#blue'))){
fixedL.css('color', 'blue');
}
else if(fixedL.anyCollided(moving)){
fixedL.css('color', 'white');
}
else{
fixedL.css('color', 'black');
}
});
});
//]]>
/* external.css */
html,body{
padding:0; margin:0;
}
body{
background:#fff; overflow-y:scroll;
}
.main{
width:940px; background:#ccc; padding:20px; margin:0 auto;
}
.fixed{
color:#000; position:fixed;
}
#fixedL{
top:50px;
}
#fixedR{
right:100px;
}
#fixedM{
bottom:10px; left:150px; border-right:35px solid #700; margin:7px 35px 25px 15px;
}
.moving {
width:400px; height:100px; background-color:#000;
}
.space{
height:200px;
}
#blue,#red{
width:700px; margin-left:150px;
}
#tiny_no_margins,#tiny_w_margins{
width:10px; height:80px;
}
#tiny_w_margins{
border:15px solid purple; margin:5px 10px 5px 250px;
}
#borders_only{
width:40px; border:25px solid brown; margin:80px;
<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<head>
<meta http-equiv='content-type' content='text/html;charset=utf-8' />
<meta name='viewport' content='width=device-width' />
<title>jQuery Collider</title>
<link type='text/css' rel='stylesheet' href='external.css' />
<script src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js'></script>
<script type='text/javascript' src='collider.js'></script>
</head>
<body>
<div class='main'>
<div id='out'>
<div id='fixedL' class='fixed'>Fixed Left</div>
<div id='fixedR' class='fixed'>Fixed Right</div>
<div id='fixedM' class='fixed'>Fixed with Border and Margins</div>
<div class='moving'></div>
<div class='space'></div>
<div id='green' class='moving'></div>
<div class='space'></div>
<div id='blue' class='moving'></div>
<div class='space'></div>
<div id='red' class='moving'></div>
<div class='space'></div>
<div id='tiny_no_margins' class='moving'></div>
<div class='space'></div>
<div id='tiny_w_margins' class='moving'></div>
<div class='space'></div>
<div id='borders_only' class='moving'></div>
<div class='space'></div>
<div class='moving'></div>
<div class='space'></div>
</div>
</div>
</body>
</html>