萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> 網頁設計

JavaScript 實現側邊欄折疊/展開:文字按鈕與圖片按鈕雙模式完整代碼

經典佈局交互:側邊欄收起展開完整實現

後台管理、文檔閱讀等界面常需「點擊收起側邊欄,騰出主內容區」功能。本文提供兩種模式完整代碼:Webdings 字體文字按鈕與圖片切換按鈕,均基於表格佈局(歷史項目常見),現代項目建議改用 Flex/Grid + CSS 動畫。

模式一:Webdings 字體文字按鈕

利用 Webdings 字體字符「3」(▶) 和「4」(◀) 作為展開/收起圖標,無需圖片請求。

<style>
.navPoint { color: white; cursor: pointer; font-family: Webdings; font-size: 9pt; }
</style>

<script>
function switchSysBar() {
  var btn = document.getElementById("switchPoint");
  var sidebar = document.getElementById("frmTitle");
  
  if (btn.innerText === "3") {  // 當前顯示 ▶,點擊後收起
    btn.innerText = "4";        // 變為 ◀
    sidebar.style.display = "none";
  } else {                      // 當前顯示 ◀,點擊後展開
    btn.innerText = "3";
    sidebar.style.display = "";
  }
}
</script>

<table border="0" cellpadding="0" cellspacing="0" height="100%" width="100%">
  <tr>
    <td width="20%" align="center" valign="middle" bgcolor="#CC99FF" id="frmTitle">側邊欄內容</td>
    <td width="9pt" class="a2">
      <table height="100%" border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td style="height:100%" onclick="switchSysBar()">
            <font style="font-size:9pt; color:#ffffff; cursor:default">
              <br/><br/><br/><br/><br/><br/><br/><br/>
              <font color="#FF0000"><br/><br/><br/><br/><br/><br/></font>
              <span class="STYLE1" id="switchPoint" title="關閉/打開左欄">3</span>
              <br/><br/><br/><br/><br/><br/><br/>
            </font>
          </td>
        </tr>
      </table>
    </td>
    <td bgcolor="#FFCC99" style="width:100%">主內容區</td>
  </tr>
</table>

模式二:圖片切換按鈕(更現代、視覺更佳)

準備兩張圖片:images/bl_1.gif(展開狀態箭頭)、images/al_1.gif(收起狀態箭頭)。

<script>
function switchSysBar() {
  var img = document.getElementById("aa");  // 圖片 ID
  var sidebar = document.getElementById("frmTitle");
  
  // 判斷當前圖片 src 包含哪個文件名
  if (img.src.indexOf("bl_1") !== -1) {  // 當前展開狀態
    img.src = "images/al_1.gif";         // 切換為收起箭頭
    sidebar.style.display = "none";
  } else {                               // 當前收起狀態
    img.src = "images/bl_1.gif";         // 切換為展開箭頭
    sidebar.style.display = "";
  }
}
</script>

<table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="770" valign="top">主內容區</td>
    <td width="10" onclick="switchSysBar()">
      <img id="aa" src="images/bl_1.gif" title="關閉/打開功能導航" />
    </td>
    <td width="191" valign="top" id="frmTitle">側邊欄內容</td>
  </tr>
</table>

現代化重構建議(Flexbox + CSS Transition)

.layout { display: flex; height: 100vh; }
.sidebar { 
  width: 200px; 
  background: #f5f5f5; 
  overflow: auto;
  transition: width 0.3s ease, opacity 0.3s ease;
}
.sidebar.collapsed { width: 0; opacity: 0; pointer-events: none; }
.main { flex: 1; overflow: auto; }
.toggleBtn { width: 20px; cursor: pointer; display: flex; align-items: center; justify-content: center; }

// JS 僅切換 class
document.querySelector('.toggleBtn').onclick = () => 
  document.querySelector('.sidebar').classList.toggle('collapsed');

核心差異:表格佈局依賴 display: none 瞬間切換,無動畫;Flex 方案配合 transition 實現平滑收展,體驗更佳,且語義化、響應式友好。

copyright © 萬盛學電腦網 all rights reserved