如何创建:折叠侧面板
- 前のページ 折りたたみ式サイドバー
- 次のページ ページネーション
学习如何创建可折叠的侧面板菜单。
创建折叠的侧面板
第一步 - 添加 HTML:
<div id="mySidepanel" class="sidepanel"> <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a> <a href="#">About</a> <a href="#">Services</a> <a href="#">Clients</a> <a href="#">Contact</a> </div> <button class="openbtn" onclick="openNav()">☰ Toggle Sidepanel</button> <h2>Collapsed Sidepanel</h2> <p>Content...</p>
第二步 - 添加 CSS:
/* 侧面板菜单 */ .sidepanel { height: 250px; /* Specify a height */ width: 0; /* 0 width - change this with JavaScript */ position: fixed; /* Stay in place */ z-index: 1; /* 上面に保つ */ top: 0; left: 0; background-color: #111; /* 黒 */ overflow-x: hidden; /* 水平スクロールを無効に */ padding-top: 60px; /* コンテンツを上部から 60px の位置に配置 */ transition: 0.5s; /* 侧面板のスライド効果の 0.5 秒遷移 */ } /* 侧面板リンク */ .sidepanel a { padding: 8px 8px 8px 32px; text-decoration: none; font-size: 25px; color: #818181; display: block; transition: 0.3s; } /* ナビゲーションリンクにマウスを合わせると色を変更 */ .sidepanel a:hover { color: #f1f1f1; } /* 閉じるボタンの位置とスタイル(右上方) */ .sidepanel .closebtn { position: absolute; top: 0; right: 25px; font-size: 36px; margin-left: 50px; } /* 侧面板開くボタンのスタイルを設定 */ .openbtn { font-size: 20px; cursor: pointer; background-color: #111; color: white; padding: 10px 15px; border: none; } .openbtn:hover { background-color: #444; }
第3ステップ - JavaScript を追加:
/* 侧边バーの幅を 250px に設定して(表示に) */ function openNav() { document.getElementById("mySidepanel").style.width = "250px"; } /* 侧边バーの幅を 0 に設定して(非表示に) */ function closeNav() { document.getElementById("mySidepanel").style.width = "0"; }
関連ページ
チュートリアル:CSSナビゲーションバー
- 前のページ 折りたたみ式サイドバー
- 次のページ ページネーション