Skip to content

Instantly share code, notes, and snippets.

@terrierscript
Created December 28, 2015 13:11
Show Gist options
  • Save terrierscript/c376e0c8fcae666df244 to your computer and use it in GitHub Desktop.
Save terrierscript/c376e0c8fcae666df244 to your computer and use it in GitHub Desktop.
Sassの@eachを使ってタブ実装をするCSS思いついた ref: http://qiita.com/inuscript/items/295616708cc18c255995
<ul id="list1">
<li>AAAA</li>
<li class="active">BBBB</li>
<li>CCCC</li>
</ul>
#list1{
li{
cursor: pointer;
display: inline-block;
border: 1px gray solid;
border-bottom: 0px;
padding: 10px;
}
.active{
background: #ccb;
}
}
- var activeTab = "bbbb"
- var tabs = ["aaaa", "bbbb", "cccc"]
ul#list1
each tab,i in tabs
li(class=(tab == activeTab ? "active" : "" ))= tab
<ul id="list2" class="active-bbbb"> <!-- 親で指定する -->
<li class="tab-aaaa">AAAA</li>
<li class="tab-bbbb">BBBB</li>
<li class="tab-cccc">CCCC</li>
</ul>
#list1{
li{
cursor: pointer;
display: inline-block;
border: 1px gray solid;
border-bottom: 0px;
padding: 10px;
}
$tabs: aaaa, bbbb, cccc; // 利用するタブを並べる
@each $tab in $tabs{
&.active-#{$tab}{
.tab-#{$tab}{ // 親と一致したらactive化する
background: #ccb;
}
}
}
}
#list1 li {
cursor: pointer;
display: inline-block;
border: 1px gray solid;
border-bottom: 0px;
padding: 10px;
}
#list1.active-aaaa .tab-aaaa {
background: #ccb;
}
#list1.active-bbbb .tab-bbbb {
background: #ccb;
}
#list1.active-cccc .tab-cccc {
background: #ccb;
}
- var activeTab = "bbbb"
- var tabs = ["aaaa", "bbbb", "cccc"]
ul#list2(class="active-" + activeTab)
each tab,i in tabs
li(class="tab-" + tab)= tab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment