Reputation: 891
HTML:
<ul class="clients">
<li>
<div class="over left">Description</div>
<div class="inner">Image</div>
</li>
</ul>
CSS:
.clients { margin-right: -20px; }
.clients li {
float: left;
width: 128px;
height: 120px;
margin: 0 20px 20px 0;
border: 1px solid #c2c2c2;
}
.clients .over {
display: none;
position: absolute;
width: 250px;
font-size: 11px;
line-height: 16px;
background: #ecf5fb;
margin: 3px 0 0 3px;
padding: 18px;
z-index: 25;
}
.clients .right { margin: 3px 0 0 -161px; }
.clients .inner { width: 128px; height: 120px; overflow: hidden; }
So, we have a list of floated squares and a popup blocks in each, which have absolute position.
JS:
jQuery(function($) {
$(".clients li").bind('mouseover mouseout',function(){$(this).find("div.over").toggle()});
});
If over - show, else - hide. Quite ok, but it must be more advanced, we should catch an offset and give a class to .over block:
.over
block..over
block.How can we do it?
Upvotes: 2
Views: 2067
Reputation: 4820
.offset()
returns an object like { left: 200, top: 300 }
$(window).width()
returns the window width
Clearly you get the left offset streight from .offset()
. The right offset you need to make a condition with should be calculated as:
offsetRight=$(window).width()-$(this).width()-$(this).offset().left;
All together:
jQuery(function($) {
$(".clients li").bind('mouseover mouseout',function(){
$over=$("div.over",this);
$over.toggle();
//didn't get if it's the li's offset you need or the actual .over, replace $(this) with $over in next lines if you need that
offset=$(this).offset();
offsetRight=$(window).width()-$(this).width()-offset.left;
if (offsetRight<150){ $over.removeClass('left').addClass('right'); }
else { $over.removeClass('right').addClass('left'); }
});
});
Upvotes: 2