如何創建:折疊側面板
學習如何創建可折疊的側面板菜單。
創建折疊的側面板
第一步 - 添加 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; /* Stay on top */ top: 0; left: 0; background-color: #111; /* Black*/ 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; }
第三步 - 添加 JavaScript:
/* 將側邊欄的寬度設置為 250px(顯示它) */ function openNav() { document.getElementById("mySidepanel").style.width = "250px"; } /* 將側邊欄的寬度設置為 0(隱藏它) */ function closeNav() { document.getElementById("mySidepanel").style.width = "0"; }
相關頁面
教程:CSS 導航欄