[CSS]ヘッダをホバーするとナビゲーションがスライド表示されるスタイルシート(スクリプト無し)
Post on:2013年7月11日
sponsorsr
スクリプトを使わずに、ヘッダをホバーすると非表示だったナビゲーションがアニメーションで表示させるスタイルシートのテクニックを紹介します。

Create a Hidden Sliding Navigation Bar Using CSS3 Transitions
デモ
デモは、CSS3アニメーション対応ブラウザでご覧ください。

デモページのアニメーション
通常時は細めのヘッダとコンテンツがあるだけで、ヘッダをホバーすると、非表示だったナビゲーションがアニメーションでスライドして表示されます。
デモのようなナビゲーションだけでなく、他のコンテンツを設置することももちろん可能です。
実装
Step 1: HTML
ヘッダ部分は最初に表示されている要素、非表示の要素の2つが配置されています。
<div id="topbar"><a href="http://designshack.net">Back to Design Shack</a> <div id="tophiddenbar"> Other Links: <a href="http://designshack.net/category/articles">Articles List</a> - <a href="http://designshack.net/gallery">Design Gallery</a> - <a href="http://designshack.net/about">About Design Shack</a> - <a href="https://twitter.com/designshack">@designshack</a> </div> </div>
Step 2: CSS
まずはCSSリセットから、box-sizingにborder-boxを指定します。
html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
outline: none;
-webkit-font-smoothing: antialiased;
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
html { height: 101%; }
body {
background: #f0f0f0 url('images/bg.gif');
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
color: #313131;
font-size: 62.5%;
line-height: 1;
}
::selection { background: #a4dcec; }
::-moz-selection { background: #a4dcec; }
::-webkit-selection { background: #a4dcec; }
::-webkit-input-placeholder { /* WebKit browsers */
color: #ccc;
font-style: italic;
}
:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
color: #ccc;
font-style: italic;
}
::-moz-placeholder { /* Mozilla Firefox 19+ */
color: #ccc;
font-style: italic;
}
:-ms-input-placeholder { /* Internet Explorer 10+ */
color: #ccc !important;
font-style: italic;
}
br { display: block; line-height: 2.2em; }
article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section { display: block; }
ol, ul { list-style: none; }
/** page structure **/
#w {
display: block;
width: 750px;
margin: 0 auto;
padding-top: 30px;
}
#content {
display: block;
width: 100%;
background: #fff;
padding: 25px 20px;
padding-bottom: 35px;
-webkit-box-shadow: rgba(0, 0, 0, 0.1) 0px 1px 2px 0px;
-moz-box-shadow: rgba(0, 0, 0, 0.1) 0px 1px 2px 0px;
box-shadow: rgba(0, 0, 0, 0.1) 0px 1px 2px 0px;
}
ヘッダのアニメーションのスタイルです。
アニメーションさせる箇所はヘッダの高さで、初期36pxから、ホバー時60pxに変更します。
#topbar {
background: #4f4a41;
padding: 10px 0 10px 0;
text-align: center;
height: 36px;
overflow: hidden;
-webkit-transition: height 0.5s linear;
-moz-transition: height 0.5s linear;
transition: height 0.5s linear;
}
#topbar a {
color: #fff;
font-size:1.3em;
line-height: 1.25em;
text-decoration: none;
opacity: 0.5;
font-weight: bold;
}
#topbar a:hover {
opacity: 1;
}
#tophiddenbar {
display: block;
width: 100%;
background: #4f4a41;
color: #b09f82;
font-weight: bold;
padding: 8px 0;
font-size: 1.3em;
text-align: center;
text-shadow: 1px 1px 0 #444;
}
#tophiddenbar a {
color: #fff;
font-size: 1.0em;
text-decoration: none;
opacity: 0.5;
text-shadow: none;
}
#tophiddenbar a:hover { opacity: 1; }
#topbar:hover { height: 60px; }
sponsors











