Skip to content

Instantly share code, notes, and snippets.

@dmitrisweb
Created December 22, 2011 16:24
Show Gist options
  • Save dmitrisweb/1510878 to your computer and use it in GitHub Desktop.
Save dmitrisweb/1510878 to your computer and use it in GitHub Desktop.
The wrapper
/* The wrapper */
.b-block {
position: relative;
display: inline-block;
overflow: hidden;
width: 10em;
height: 10em;
vertical-align: middle;
-webkit-transform: translateZ(0);
}
/* The blocks that would be hovered */
.b-block__hoverer {
position: absolute;
z-index: 1;
width: 71%;
height: 71%;
transform: rotate(45deg);
}
/* Positioning for hoverers */
.b-block__hoverer:nth-child(1) {
top: 0;
right: 0;
transform-origin: 100% 0;
}
.b-block__hoverer:nth-child(2) {
bottom: 0;
right: 0;
transform-origin: 100% 100%;
}
.b-block__hoverer:nth-child(3) {
bottom: 0;
left: 0;
transform-origin: 0 100%;
}
.b-block__hoverer:nth-child(4) {
top: 0;
left: 0;
transform-origin: 0 0;
}
/* Enlarge the hoverer to cover the whole area */
.b-block__hoverer:hover {
z-index: 9;
width: 100%;
height: 100%;
transform: none;
}
/* Styles for content block */
.b-block__content {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
text-align: center;
line-height: 10em;
background: #333;
color: #FFF;
/* Restore the antialiasing lost from translateZ */
-webkit-font-smoothing: subpixel-antialiased;
transition: all .3s ease;
box-shadow: 0 -10em 0 0 red, 10em 0 0 0 blue, 0 10em 0 0 lime, -10em 0 0 0 orange;
}
/* Move the content block while hovering different blocks */
.b-block__hoverer:nth-child(1):hover ~ .b-block__content {
transform: translate(0, 100%);
}
.b-block__hoverer:nth-child(2):hover ~ .b-block__content {
transform: translate(-100%, 0);
}
.b-block__hoverer:nth-child(3):hover ~ .b-block__content {
transform: translate(0, -100%);
}
.b-block__hoverer:nth-child(4):hover ~ .b-block__content {
transform: translate(100%, 0);
}
/* Just for aestetics */
BODY {
padding: 2em;
font: 16px/1.5 "Helvetica Neue", Arial, sans-serif;
text-align: center;
}
<p>Try to hover this block from different directions</p>
<p>↓</p>
<span>→ </span>
<div class="b-block">
<div class="b-block__hoverer"></div>
<div class="b-block__hoverer"></div>
<div class="b-block__hoverer"></div>
<div class="b-block__hoverer"></div>
<div class="b-block__content">
Hover me!
</div>
</div>
<span> ←</span>
<p>↑</p>
{"view":"split","prefixfree":"1","page":"css"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment