Если прямоугольники параллельны осям.
<script type="text/javascript">
window.onload = function() {
var box1 = document.getElementById("box1");
var box2 = document.getElementById("box2");
function rect2Rect(obj1, obj2) {
return ( obj1.offsetLeft <= obj2.offsetLeft + obj2.offsetWidth && obj1.offsetLeft + obj1.offsetWidth >= obj2.offsetLeft && obj1.offsetTop + obj1.offsetHeight >= obj2.offsetTop && obj1.offsetTop <= obj2.offsetTop + obj2.offsetHeight );
}
function move() {
if(!rect2Rect(box1, box2)) {
setTimeout(function() {
box1.style.left = box1.offsetLeft + 5 + "px";
box2.style.top = box2.offsetTop - 5 + "px";
move();
}, 50);
}
else{
alert("Столкновение");
}
}
move();
}
</script>
<div id="box1" style=" width:30px; height:30px; background-color:#0099FF; position:absolute; left:10px"></div>
<div id="box2" style=" width:30px; height:30px; background-color:#CCCC00; position:absolute; left:100px; top:100px; "></div>