[CSS]ol要素を美しくスタイルし、アニメーションをちょっとだけ加えるテクニック
Post on:2012年2月20日
sponsorsr
擬似要素を使って、ol要素を美しくスタイルし、CSS3アニメーションをちょっとだけ加えたスタイルシートのテクニックを紹介します。

[ad#ad-2]
デモ
デモはIE9, Firefox, Chrome, Safariでご覧ください。
擬似要素のアニメーションは、Firefoxのみサポートしています。


デモページ:Demo 1
数字をサークル状にスタイルし、ホバーで数字がくるくるっと回転します。
※アニメーションはFirefoxのみです。
[ad#ad-2]

デモページ:Demo 1
数字を矩形にスタイルし、ホバーで数字からアローが伸びます。
※アニメーションはFirefoxのみです。
実装
HTML
HTMLは非常にシンプルで、ol要素を入れ子にして実装するだけです。
<ol class="rounded-list"> <li><a href="">List item</a></li> <li><a href="">List item</a></li> <li><a href="">List item</a> <ol> <li><a href="">List sub item</a></li> <li><a href="">List sub item</a></li> <li><a href="">List sub item</a></li> </ol> </li> <li><a href="">List item</a></li> <li><a href="">List item</a></li> </ol>
CSS
まずは、共通のスタイルシートから。
ここで使用するのは、入れ子のol要素にカウンターのリセットをしナンバリングするテクニックです。カウンターのリセットにはCSS2.1の「counter-reset」を使用します。
ol{
counter-reset: li; /* Initiate a counter */
list-style: none; /* Remove default numbering */
*list-style: decimal; /* Keep using default numbering for IE6/7 */
font: 15px 'trebuchet MS', 'lucida sans';
padding: 0;
margin-bottom: 4em;
text-shadow: 0 1px 0 rgba(255,255,255,.5);
}
ol ol{
margin: 0 0 0 2em; /* Add some left margin for inner lists */
}
あとは、2つのデモでスタイルが異なるので、キャプチャと共にご紹介。

デモページ:Demo 1
擬似要素を使って、数字をサークル状にスタイルします。
.rounded-list a{
position: relative;
display: block;
padding: .4em .4em .4em 2em;
*padding: .4em;
margin: .5em 0;
background: #ddd;
color: #444;
text-decoration: none;
border-radius: .3em;
transition: all .3s ease-out;
}
.rounded-list a:hover{
background: #eee;
}
.rounded-list a:hover:before{
transform: rotate(360deg);
}
.rounded-list a:before{
content: counter(li);
counter-increment: li;
position: absolute;
left: -1.3em;
top: 50%;
margin-top: -1.3em;
background: #87ceeb;
height: 2em;
width: 2em;
line-height: 2em;
border: .3em solid #fff;
text-align: center;
font-weight: bold;
border-radius: 2em;
transition: all .3s ease-out;
}

デモページ:Demo 1
数字を矩形状にスタイルし、ホバー時に三角のアローを表示します。
.rectangle-list a{
position: relative;
display: block;
padding: .4em .4em .4em .8em;
*padding: .4em;
margin: .5em 0 .5em 2.5em;
background: #ddd;
color: #444;
text-decoration: none;
transition: all .3s ease-out;
}
.rectangle-list a:hover{
background: #eee;
}
.rectangle-list a:before{
content: counter(li);
counter-increment: li;
position: absolute;
left: -2.5em;
top: 50%;
margin-top: -1em;
background: #fa8072;
height: 2em;
width: 2em;
line-height: 2em;
text-align: center;
font-weight: bold;
}
.rectangle-list a:after{
position: absolute;
content: '';
border: .5em solid transparent;
left: -1em;
top: 50%;
margin-top: -.5em;
transition: all .3s ease-out;
}
.rectangle-list a:hover:after{
left: -.5em;
border-left-color: #fa8072;
}
対応ブラウザ
現在擬似要素のアニメーションをサポートしているブラウザはFirefoxのみです。IE9, Chrome, Safariなどではアニメーション無しで動作します。
また、もっと古いブラウザでは下記のように表示されます。

IE6/7/8での表示
sponsors











