Skip to content

Instantly share code, notes, and snippets.

@shinnn
Last active December 10, 2015 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinnn/4502883 to your computer and use it in GitHub Desktop.
Save shinnn/4502883 to your computer and use it in GitHub Desktop.
Setting CSS sprite and :hover effect, in Sass
// background.jpg
//
// +------+------+
// |#aaa |#aaa |
// | |:hover|
// +------+------+
// |#bbb |#bbb |
// | |:hover|
// +------+------+
// |#ccc |#ccc |
// | |:hover|
// +------+------+
$currentSpriteY : 0px;
@mixin horizontalHover($elm, $singleWidth, $singleHeight){
#{$elm}{
background-position: 0px $currentSpriteY;
a:hover{
background-position: -$singleWidth $currentSpriteY;
}
}
$currentSpriteY : $currentSpriteY - $singleHeight;
}
// Usage Example
@each $elm in "#aaa", "#bbb", "#ccc"{
@include horizontalHover($elm, 80px, 40px);
}
// background.jpg
//
// +------+------+------+
// |#aaa |#bbb |#ccc |
// | | | |
// +------+------+------+
// |#aaa |#bbb |#ccc |
// |:hover|:hover|:hover|
// +------+------+------+
$currentSpriteX : 0px;
@mixin verticalHover($elm, $singleWidth, $singleHeight){
#{$elm}{
background-position: $currentSpriteY 0px;
a:hover{
background-position: $currentSpriteY -$singleHeight;
}
}
$currentSpriteX : $currentSpriteX - $singleWidth;
}
// Usage Example
@each $elm in "#aaa", "#bbb", "#ccc"{
@include verticalHover($elm, 40px, 80px);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment