You can do this in CSS itself, using 'hover' selector and 'nth-of-type'.
Select every third-in-row:
:nth-of-type(3), :nth-of-type(7), :nth-of-type(13), :nth-of-type(15)
and fourth-in-row:
:nth-of-type(4), :nth-of-type(8), :nth-of-type(12), :nth-of-type(16)
And give its fly-out box the necessary properties.
Demo: http://dabblet.com/gist/3765761
Markup:
<div class='box'>1
<div class='fly-out'></div>
</div>
<div class='box'>2
<div class='fly-out'></div>
</div>
<div class='box right-aligned'>3
<div class='fly-out'></div>
</div>
<div class='box right-aligned'>4
<div class='fly-out'></div>
</div>
<div class='box'>5
<div class='fly-out'></div>
</div>
<div class='box'>6
<div class='fly-out'></div>
</div>
<div class='box right-aligned'>7
<div class='fly-out'></div>
</div>
<div class='box right-aligned'>8
<div class='fly-out'></div>
</div>
<div class='box'>9
<div class='fly-out'></div>
</div>
<div class='box'>10
<div class='fly-out'></div>
</div>
<div class='box right-aligned'>11
<div class='fly-out'></div>
</div>
<div class='box right-aligned'>12
<div class='fly-out'></div>
</div>
<div class='box'>13
<div class='fly-out'></div>
</div>
<div class='box'>14
<div class='fly-out'></div>
</div>
<div class='box right-aligned'>15
<div class='fly-out'></div>
</div>
<div class='box right-aligned'>16
<div class='fly-out'></div>
</div>
css:
* {
margin: 0;
padding: 0;
box-sizing: border-box; /* paulirish.com/2012/box-sizing-border-box-ftw/ */
position: relative;
}
body {
width: 440px;
margin: 0 auto;
}
.box {
width: 100px;
height: 100px;
border: 1px solid #999;
float: left;
margin: 5px;
}
.fly-out {
position: absolute;
top: 0;
background: rgba(0,0,0,.4);
opacity: 0;
z-index: -1;
}
.box:hover .fly-out {
z-index: 1;
opacity: 1;
height: 100%;
width: 210px;
}
.box:nth-of-type(3):hover .fly-out,
.box:nth-of-type(7):hover .fly-out,
.box:nth-of-type(11):hover .fly-out,
.box:nth-of-type(15):hover .fly-out,
.box:nth-of-type(4):hover .fly-out,
.box:nth-of-type(8):hover .fly-out,
.box:nth-of-type(12):hover .fly-out,
.box:nth-of-type(16):hover .fly-out,
.right-aligned {
left: -110px;
right: 0;
background: rgba(0,0,120,.7);
}
In your markup, the 'tool-tip' was outside the box and it had 'visibility: hidden' which is pretty much like giving it 'opacity: 0'. While it can't be seen, it's still there. To actually make it unavailable to the layout, you need 'display: none', but then you wont be able to animate it properly using CSS transitions.
Note: Like Christofer Eliason mentioned in the comment below, the ':nth-of-type' selector is not very well supported, so you can either add a class to every div you want the fly-out to be right aligned or look into using a jquery version of 'nth-of-type': https://stackoverflow.com/a/4761510/604040.
I used the ':nth-of-type' selector because you can control the fly-out's alignment from the CSS itself without having to add a class to new boxes if they are added dynamically, this can be a tad complex mathematically(at least for me).
But if you're fine with it, I've added a class '.right-aligned' to the boxes in 3rd & 4th in each row, you can delete this block in the CSS and you're still good:
.box:nth-of-type(3):hover .fly-out,
.box:nth-of-type(7):hover .fly-out,
.box:nth-of-type(11):hover .fly-out,
.box:nth-of-type(15):hover .fly-out,
.box:nth-of-type(4):hover .fly-out,
.box:nth-of-type(8):hover .fly-out,
.box:nth-of-type(12):hover .fly-out,
.box:nth-of-type(16):hover .fly-out