ghoullier
1/15/2014 - 1:47 PM

compass-retina-sprites.scss

@import "compass/css3/background-size";

$sprites: sprite-map("social/*.png");
$sprites-retina: sprite-map("retina/*.png");

@mixin sprite-background($name) {
  background-image: sprite-url($sprites);
  background-position: sprite-position($sprites, $name);
  background-repeat: no-repeat;
  display: block;
  height: image-height(sprite-file($sprites, $name));
  width: image-width(sprite-file($sprites, $name));
  @media (-webkit-min-device-pixel-ratio: 2), (-o-min-device-pixel-ratio: 3/2), (min-device-pixel-ratio: 2) {
    // Workaround for https://gist.github.com/2140082
    @if (sprite-position($sprites, $name) != sprite-position($sprites-retina, $name)) {
      $ypos: round(nth(sprite-position($sprites-retina, $name), 2) / 2);
      background-position: 0 $ypos;
    }
    // Hard coded width of the normal sprite image. There must be a smarter way to do this.
    @include background-size(image-width(sprite-path($sprites)) image-height(sprite-path($sprites)));
    background-image: sprite-url($sprites-retina);
  }
}

// Usage.
.facebook-icon {
  @include sprite-background(facebook);
}