【Labs】ヘッダーをトップに固定してスクロールでアニメーション
こんにちは(・∀・)
今回はjQueryでヘッダーをトップに固定するスティッキーヘッダーと、スクロールするとそのヘッダーがCSSのアニメーションで拡大縮小するサンプルをご紹介します。
レスポンシブデザインに対応してます。
サンプル
HTML
jQueryを使用するのでHTMLの<head>
内にjQuery 1.x snippetを読み込みます。
Google Hosted Libraries
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<header>
<p><img src="logo.png" alt="Webデザインラボ"></p>
</header>
CSS
header {
position: absolute;
background: #ccc;
top: 0;
width: 100%;
height: 60px;
border-bottom: none;
box-shadow: 0 1px 2px #aaa;
}
header #toplogo {
margin-top: 0;
}
@keyframes anim1 {
0% { height: 60px; }
100% { height: 40px; }
}
@keyframes anim2 {
0% { height: 40px; }
100% { height: 60px; }
}
@keyframes anim3 {
0% { width: 200px; height: 40px; }
100% { width: 100px; height: 20px; }
}
@keyframes anim4 {
0% { width: 100px; height: 20px; }
100% { width: 200px; height: 40px; }
}
.fixed {
position: fixed;
height: 40px;
animation: 0.5s forwards anim1;
text-align: center;
}
.fixed2 {
position: fixed;
height: 60px;
animation: 0.2s backwards anim2;
text-align: center;
}
.fixed section h2 img {
width: 100px;
height: 20px;
animation: 0.5s forwards anim3;
}
.fixed2 section h2 img {
width: 200px;
height: 40px;
animation: 0.2s backwards anim4;
}
.sample-demo {
margin: 0;
padding: 0;
}
h3 {
margin-top: 60px;
margin-bottom: 10px;
}
.container {
position: relative;
overflow: hidden;
font-size: 28px;
color: #333;
box-sizing: border-box;
}
.container .box1 {
position: relative;
min-height: 100vh;
box-sizing: border-box;
text-align: left;
padding: 10px;
background: #fff;
}
Result
関連リンク
【Labs】スクロールしても要素が固定するスティッキーサイドバー
【Labs】グローバルナビをトップに固定するスティッキーナビ+ドロップダウンメニュー
【Labs】グローバルナビをトップに固定するスティッキーナビ