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

【Compass】Retina対応のスプライト関数で作る画像グローバルナビmixin【Sass】

More than 3 years have passed since last update.

画像を使うGrobalNaviに使える。

*マジックインポートで画像を一枚にまとめる方法では、Retina対応をさせるためサイズ縮小する際比率がおかしくなるので、まとめずに一枚画像で呼び出しています。

使用する画像を用意

スクリーンショット 2016-03-31 20.01.48.png

とりあえず3つ用意。
hover時の画像は_hoverと付けておく。

Sass

あらかじめ「config.rb」の方で画像のリンク先「images_dir」を指定しておく。

SCSS
@import "compass";//compassを呼び出す

$gbnav-map: sprite-map("gbnav/*.png");//gbnavフォルダの画像を読み込み
$gbnav-size:image-width(sprite-file($gbnav-map,nav01));
//gbnav画像サイズの原寸を計るためnav01.pngを呼び出している。

// 繰り返し処理
//=================================

$value: 1;
$i: 2;//サイズを半分にする

@while  $value <= 3 {//gbnavの数
    .nav0#{$value}{
        background: image-url("gbnav/nav0#{$value}.png") no-repeat{
            size: ($gbnav-size / $i) ($gbnav-size / $i);
        }
        &:hover{
            background: image-url("gbnav/nav0#{$value}_hover.png") no-repeat{
                size: ($gbnav-size / $i) ($gbnav-size / $i);
            }
        }
    }
    $value: $value + 1;
}

// .gbNav
//=================================

.gbNav{
    width: 960px;
    ul{
        @include horizontal-list(0px);//横並びのcompass関数
        li{
            width:$gbnav-size / $i;
            height:$gbnav-size / $i;
            cursor: pointer;
        }
    }
}

width:800pxの画像なら$iで半分にするため2の値を入れているので、width:400pxで表示される。
その都度縮小したい値を$iに入れる。
*horizontal-listはcompassの関数

コンパイル後CSS

CSS
.nav01 {
  background: url('../image/gbnav/nav01.png?1459140518') no-repeat;
  background-size: 12px 12px;
}
.nav01:hover {
  background: url('../image/gbnav/nav01_hover.png?1459150477') no-repeat;
  background-size: 12px 12px;
}

.nav02 {
  background: url('../image/gbnav/nav02.png?1459140510') no-repeat;
  background-size: 12px 12px;
}
.nav02:hover {
  background: url('../image/gbnav/nav02_hover.png?1459150489') no-repeat;
  background-size: 12px 12px;
}

.nav03 {
  background: url('../image/gbnav/nav03.png?1459140494') no-repeat;
  background-size: 12px 12px;
}
.nav03:hover {
  background: url('../image/gbnav/nav03_hover.png?1459150498') no-repeat;
  background-size: 12px 12px;
}

.gbNav {
  width: 960px;
}
.gbNav ul {
  margin: 0;
  padding: 0;
  border: 0;
  overflow: hidden;
  *zoom: 1;
}
.gbNav ul li {
  list-style-image: none;
  list-style-type: none;
  margin-left: 0;
  white-space: nowrap;
  float: left;
  padding-left: 0px;
  padding-right: 0px;
}
.gbNav ul li:first-child {
  padding-left: 0;
}
.gbNav ul li:last-child {
  padding-right: 0;
}
.gbNav ul li.last {
  padding-right: 0;
}
.gbNav ul li {
  width: 12px;
  height: 12px;
  -webkit-transition: all 0.3s ease-in;
  -moz-transition: all 0.3s ease-in;
  -ms-transition: all 0.3s ease-in;
  -o-transition: all 0.3s ease-in;
  transition: all 0.3s ease-in;
  cursor: pointer;
}

HTML

HTML
<div class="gbNav">
    <ul>
        <li class="nav01"><a href=""></a></li>
        <li class="nav02"><a href=""></a></li>
        <li class="nav03"><a href=""></a></li>
    </ul>
</div>
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
ユーザーは見つかりませんでした