Created
September 17, 2012 13:31
-
-
Save steveu/3737271 to your computer and use it in GitHub Desktop.
Sass/SCSS Loops
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given the following html | |
<nav> | |
<ul> | |
<li class="home"><a>Home</a></li> | |
<li class="about"><a>About</a></li> | |
<li class="blog"><a>Blog</a></li> | |
<li class="contact"><a>Contact</a></li> | |
</ul> | |
</nav> | |
*/ | |
/* | |
Create collections with corresponding items | |
*/ | |
$nav_section: home about blog contact; | |
$nav_widths: 80 120 100 140; | |
$nav_colors: red yellow blue green; | |
/* | |
Then using a loop and referencing the separate collection using nth | |
create an approximation of an associative array | |
*/ | |
nav { | |
li { | |
$i:1; // count | |
$bg_position: 0; // background position count (only for BG img) | |
@each $section in $nav_section { | |
&.#{$section} a { | |
width: nth($nav_widths, $i) + px; | |
background-color: nth($nav_colors, $i); | |
// if using a background image | |
background-position: -$bg_position + px 0px; | |
} | |
// add width to position counter | |
$bg_position: $bg_position + nth($nav_widths, $i); | |
// iterate count | |
$i: $i+1; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment