Skip to content

Instantly share code, notes, and snippets.

@jimyuan
Last active August 29, 2015 14:15
Show Gist options
  • Save jimyuan/85d0ae8e5909bfb0faf7 to your computer and use it in GitHub Desktop.
Save jimyuan/85d0ae8e5909bfb0faf7 to your computer and use it in GitHub Desktop.
compass css sprite for x or 2x background image
//----------------------------------------------------//
// compass css sprite for x or 2x or 3x background image
//----------------------------------------------------//
$sprites-spacing: 5px;
@import "icons/*.png"; // got a variable named $icons-sprites
// @import "icons-2x/*.png"; // got a variable named $icons-2x-sprites
// @import "icons-3x/*.png"; // got a variable named $icons-3x-sprites
@mixin get-sprite( $map, $sprite, $x: 1, $adjustX: 0, $adjustY: 0, $positionAdjustY: 0, $repeat: no-repeat ) {
//For use in media queries. Regular @import <dir>-sprite(file-name) does not fully work.
$sprite-image: sprite-file( $map, $sprite );
$sprite-map: sprite-url( $map );
$sprite-position: sprite-position( $map, $sprite );
//return background
background: $sprite-map $sprite-position $repeat;
$div: 1;
@if $x > 1 {
$div: $x;
$map-path: sprite-path( $map );
@include background-size( ceil( image-width( $map-path )/$div ) auto);
//return position to override above position returned in 'background:'
background-position: nth( $sprite-position, 1 ) ( ( nth($sprite-position, 2 )+( $adjustY*2 ) )/$div )-$positionAdjustY;
}
$sprite-width: image-width( $sprite-image );
$sprite-height: image-height( $sprite-image );
//return width
width: ( $sprite-width/$div )+$adjustX;
//return height
height: ( $sprite-height/$div )+$adjustY;
}
.icon-demo {
@include get-sprite($icons-sprites, icon-retry, true, 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment