Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
9
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

Compass Sprite用のmixin

Compassでスプライトを使うときのmixin。

$device-pixel-ratioをいじればRetina対応でもそうでないやつでもいける感じです。

// $name: sprite file name
// $sprites: sprite-map
// $block: display type
// $device-pixel-ratio: if use Retina-image, it should 2 and original-image-size is x2.
// $sprites-image: sprite-url($sprite) ;for optimize compile

@mixin sprite-bg-size($name, $sprites, $device-pixel-ratio: 2) {
    $_w: image-width(sprite-file($sprites, $name)) / $device-pixel-ratio;
    $_h: image-height(sprite-file($sprites, $name)) / $device-pixel-ratio;
    width: $_w;
    height: $_h;
    background-size: $_w auto;
}

@mixin sprite-bg-position($name, $sprites, $device-pixel-ratio: 2) {
    $_pos: sprite-position($sprites, $name);
    $_x: round(nth($_pos, 1) / $device-pixel-ratio);
    $_y: round(nth($_pos, 2) / $device-pixel-ratio);
    background-position: $_x $_y;
}

@mixin sprite-bg-image($sprites, $sprites-image, $display: block) {
  display: $display;
  background-image: $sprites-image;
  background-repeat: no-repeat;
}

// manager
@mixin sprite-bg($name, $sprites, $sprites-image, $display: block, $device-pixel-ratio: 2) {
  @include sprite-bg-image($sprites, $sprites-image, $display);
  @include sprite-bg-position($name, $sprites, $device-pixel-ratio);
  @include sprite-bg-size($name, $sprites, $device-pixel-ratio);
}

@mixin sprite-bg-base($name, $sprites, $sprites-image, $display: block, $device-pixel-ratio: 2) {
    @include sprite-bg-image($sprites, $sprites-image, $display);
    @include sprite-bg-size($name, $sprites, $device-pixel-ratio);
}

使うとき

$sprites: sprite-map("hoge/fuga/*.png", $layout: smart);
$spritesImage: sprite-url($sprites);
// hoge/fuga/以下に default.pngとon.pngがあると想定して
.piyo {
    @include sprite-bg-base('default', $sprites, $spritesImage);
    @include sprite-bg-position('default', $sprites);
    &:hover {
        @include sprite-bg-position('on', $sprites);
    }
}

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
9
Help us understand the problem. What are the problem?