3月更新・前月(2月)の人気記事トップ10 03/01/2023
- ( 01 – ) 【iPhone / iPad】iPhoneでSuicaをクレジットカード無しで使う方法
- ( 02 – ) 【Mac】MacにGoogle Driveをインストール
- ( 03 – ) 【iPhone / iPad】iPhoneのツイッターアプリでユーザー名をコピーする方法
- ( 06 ↑) 【jQuery】入門2. jQueryをHTMLに組み込む
- ( 05 – ) 【Labs】position:absoluteとwidth:100%を指定すると横幅の設定がうまくいかない場合の対処方法について
- ( 05 ↓) 【Mac】Macのユーザー名とアカウント名を変更する
- ( 07 – ) 【Mac】横画面で撮影した動画をYouTubeショート用にMacのiMovieで縦画面に編集する方法
- ( 08 – ) 【iPhone iPad】iPhoneやiPadの音量を細かく調整する方法
- (圏外↑) 【Labs】マウスオーバーでボタンの色が変わるロールオーバー
- ( 09 ↓) 【jQuery】入門5. jQueryで日時を表示
【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】グローバルナビをトップに固定するスティッキーナビ