Help us understand the problem. What is going on with this article?

Compass Sprite用のmixin

More than 5 years have passed since last update.

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
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした