Skip to content

Instantly share code, notes, and snippets.

@nathos
Last active September 25, 2015 17:47
Show Gist options
  • Save nathos/959764 to your computer and use it in GitHub Desktop.
Save nathos/959764 to your computer and use it in GitHub Desktop.
Compass fancy-hover - snazzy-looking image replacement hovers with an animated opacity ramp. Now using Compass sprites so you don't have to do the dirty work! See the demo at http://nathos.github.com/fancy-hovers/
@mixin fancy-hover($sprite_dir, $off_state, $hover_state, $speed: 0.3s)
$sprites: sprite-map("#{$sprite_dir}/*.png")
$width: image-width(sprite_file($sprites, $off_state))
$height: image-height(sprite_file($sprites, $off_state))
@include hide-text
width: $width
height: $height
background: sprite($sprites, $off_state) no-repeat
display: block
position: relative
span
width: $width
height: $height
background: sprite($sprites, $hover_state) no-repeat
display: block
position: absolute
top: 0
left: 0
opacity: 0
@include transition(opacity $speed ease-in-out)
.no-opacity &
visibility: hidden
&:hover span
opacity: 1
.no-opacity &
visibility: visible
<div class="fancy-hover-links">
<ul>
<li id='twitter'>
Twitter
<span></span>
<!-- don't forget this empty span! -->
</li>
<li id='facebook'>
Facebook
<span></span>
</li>
<li id='forrst'>
Forrst
<span></span>
</li>
<li id='rss'>
RSS
<span></span>
</li>
</ul>
</div>
// in this example, the "my_sprites" directory contains 8 pngs, 2 for each image
#twitter
// pass the sprite directory, the off state name, and the hover state name
// you can optionally pass a fourth argument for the duration of the fade
@include fancy-hover(my_sprites, twitter_off, twitter_on)
#facebook
// repeat for each pair of sprites you want to make fancy!
@include fancy-hover(my_sprites, facebook_off, facebook_on)
#forrst
@include fancy-hover(my_sprites, forrst_off, forrst_on)
#rss
@include fancy-hover(my_sprites, rss_off, rss_on)
@nathos
Copy link
Author

nathos commented Feb 21, 2013

@Elloro: my apologies, there was a change in Compass a while back that changes the syntax of the transition mixin to take multiple transitions at once. This means you can either use the single-transition mixin instead, or change the list passed to transition to be space-separated.

I've updated the Gist with the latter, like so:

@include transition(opacity $speed ease-in-out)

@Elloro
Copy link

Elloro commented Feb 25, 2013

YES did the trick! Thnx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment