使用圖示建立 Angular 可展開和可折疊側邊欄
在 Angular 中建立可擴展和可折疊的側邊欄可以顯著增強應用程式的使用者體驗。本教程提供了建立此類側邊欄的逐步指南,其中包含圖示和平滑過渡。我們將涵蓋從設定元件結構到應用程式樣式和邏輯來切換側邊欄的所有內容。
為什麼要使用可折疊側邊欄?
可折疊側邊欄透過以下方式提高應用程式的可用性:
- 節省螢幕空間。
- 提供輕鬆的導航。
- 保持介面整潔有序。
建立側邊欄的分步指南
1. 設定你的 Angular 項目
首先,請確保您安裝了 Angular CLI。如果沒有,請執行:
npm install -g @angular/cli
建立一個新的 Angular 專案:
ng new angular-sidebar cd angular-sidebar
產生必要的組件:
ng generate component sidebar
2. 定義側邊欄結構
app.component.html
這將作為應用程式的主容器。新增側邊欄和用於切換其狀態的按鈕:
<div> <h4> <strong>app.component.ts</strong> </h4> <p>Add the logic to manage the sidebar's state:<br> </p> <pre class="brush:php;toolbar:false">import { Component } from '@angular/core'; @Component({ selector: 'my-app', templateUrl: './app.component.html', styleUrls: ['./app.component.scss'], }) export class AppComponent { isSidebarCollapsed = false; onSidebarToggle() { this.isSidebarCollapsed = !this.isSidebarCollapsed; } }
3. 實作側邊欄組件
sidebar.component.html
定義側邊欄的 HTML 結構,包括帶有巢狀項目的選單:
<div> <h4> <strong>sidebar.component.ts</strong> </h4> <p>Handle the toggle logic for menu items and sidebar:<br> </p> <pre class="brush:php;toolbar:false">import { Component, EventEmitter, Input, Output } from '@angular/core'; interface MenuItem { icon: string; label: string; children?: MenuItem[]; isOpen?: boolean; } @Component({ selector: 'app-sidebar', templateUrl: './sidebar.component.html', styleUrls: ['./sidebar.component.scss'], }) export class SidebarComponent { @Input() isSidebarCollapsed = false; @Output() sidebarToggle = new EventEmitter<void>(); menuItems: MenuItem[] = [ { icon: 'fas fa-home', label: 'Dashboard', isOpen: false, children: [ { icon: 'fas fa-chart-pie', label: 'Analytics' }, { icon: 'fas fa-tasks', label: 'Projects' }, ] }, { icon: 'fas fa-cog', label: 'Settings', isOpen: false, children: [ { icon: 'fas fa-user', label: 'Profile' }, { icon: 'fas fa-lock', label: 'Security' }, ] }, { icon: 'fas fa-envelope', label: 'Messages' } ]; toggleSidebar() { this.sidebarToggle.emit(); } toggleMenuItem(item: MenuItem) { // Only toggle if sidebar is not collapsed and item has children if (!this.isSidebarCollapsed && item.children) { item.isOpen = !item.isOpen; } } } </void>
4. 設定側邊欄的樣式
app.component.scss
新增版面配置和轉場的全域樣式:
.app-container { display: flex; height: 100vh; overflow: hidden; } .content { flex-grow: 1; margin-left: 250px; transition: all 0.3s ease-in-out; background-color: #f4f6f7; overflow-y: auto; &-inner { padding: 2rem; max-width: 1200px; margin: 0 auto; } &-expanded { margin-left: 50px; } } .sidebar-toggle-btn { position: absolute; top: 1rem; left: 200px; // Default position when sidebar is expanded background-color: #2c3e50; border: none; color: #fff; padding: 0.5rem; border-top-right-radius: 0.5rem; border-bottom-right-radius: 0.5rem; cursor: pointer; z-index: 1001; box-shadow: 2px 0 5px rgba(0, 0, 0, 0.1); transition: all 0.3s ease; &:hover { background-color: #34495e; } &.sidebar-collapsed { left: 15px; // Position when sidebar is collapsed } }
sidebar.component.scss
定義側邊欄和選單的樣式:
.sidebar { background-color: #2c3e50; color: #ecf0f1; height: 100vh; width: 250px; position: fixed; top: 0; left: 0; z-index: 1000; transition: all 0.3s ease-in-out; overflow-x: hidden; box-shadow: 2px 0 5px rgba(0, 0, 0, 0.1); } .sidebar-header { display: flex; justify-content: center; align-items: center; padding: 1.5rem; position: relative; } .sidebar-logo { color: #fff; text-decoration: none; font-size: 1.5rem; font-weight: bold; text-align: center; } .sidebar-menu { padding: 1rem 0; ul { list-style-type: none; padding: 0; margin: 0; } } .sidebar-menu-item { position: relative; } .sidebar-item { display: flex; align-items: center; color: #ecf0f1; text-decoration: none; padding: 0.75rem 1rem; transition: all 0.2s ease; cursor: pointer; &:hover { background-color: rgba(255, 255, 255, 0.1); } &.menu-item-active { background-color: rgba(255, 255, 255, 0.2); } i { margin-right: 0.75rem; &.sidebar-item-arrow { margin-left: auto; font-size: 0.8rem; transition: transform 0.3s ease; &.rotated { transform: rotate(180deg); } } } &-text { opacity: 1; transition: opacity 0.3s ease-in-out; } &.has-children { position: relative; } } .sidebar-submenu { background-color: rgba(0, 0, 0, 0.1); .sidebar-item { padding-left: 3rem; font-size: 0.9rem; } } .sidebar-collapsed { width: 50px; .sidebar-menu-item { position: static; } .sidebar-item { i { margin-right: 0; } &-text, &-arrow { opacity: 0; width: 0; overflow: hidden; } } .sidebar-submenu { display: none; } }
5. 運行應用程式
啟動開發伺服器:
ng serve
導覽至 http://localhost:4200/ 以查看正在執行的側邊欄。
常見問題解答
如何自訂側邊欄圖示?
修改 sidebar.component.ts 中的 menuItems 陣列並提供適當的圖示類別。
可以加入選單展開動畫嗎?
是的,使用 Angular 的動畫模組在選單開啟和關閉時添加平滑的過渡。
如何調整側邊欄寬度?
更新 sidebar.component.scss 中展開和折疊狀態的寬度屬性。
本指南涵蓋了在 Angular 中建立功能性可擴充和可折疊側邊欄的所有基本步驟。您可以進一步客製化設計和功能以滿足您的應用需求。
以上是帶有圖示的 Angular 可展開和可折疊側邊欄的分步指南的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Python和JavaScript在開發環境上的選擇都很重要。 1)Python的開發環境包括PyCharm、JupyterNotebook和Anaconda,適合數據科學和快速原型開發。 2)JavaScript的開發環境包括Node.js、VSCode和Webpack,適用於前端和後端開發。根據項目需求選擇合適的工具可以提高開發效率和項目成功率。

是的,JavaScript的引擎核心是用C語言編寫的。 1)C語言提供了高效性能和底層控制,適合JavaScript引擎的開發。 2)以V8引擎為例,其核心用C 編寫,結合了C的效率和麵向對象特性。 3)JavaScript引擎的工作原理包括解析、編譯和執行,C語言在這些過程中發揮關鍵作用。

JavaScript是現代網站的核心,因為它增強了網頁的交互性和動態性。 1)它允許在不刷新頁面的情況下改變內容,2)通過DOMAPI操作網頁,3)支持複雜的交互效果如動畫和拖放,4)優化性能和最佳實踐提高用戶體驗。

C 和JavaScript通過WebAssembly實現互操作性。 1)C 代碼編譯成WebAssembly模塊,引入到JavaScript環境中,增強計算能力。 2)在遊戲開發中,C 處理物理引擎和圖形渲染,JavaScript負責遊戲邏輯和用戶界面。

JavaScript在網站、移動應用、桌面應用和服務器端編程中均有廣泛應用。 1)在網站開發中,JavaScript與HTML、CSS一起操作DOM,實現動態效果,並支持如jQuery、React等框架。 2)通過ReactNative和Ionic,JavaScript用於開發跨平台移動應用。 3)Electron框架使JavaScript能構建桌面應用。 4)Node.js讓JavaScript在服務器端運行,支持高並發請求。

Python更適合數據科學和自動化,JavaScript更適合前端和全棧開發。 1.Python在數據科學和機器學習中表現出色,使用NumPy、Pandas等庫進行數據處理和建模。 2.Python在自動化和腳本編寫方面簡潔高效。 3.JavaScript在前端開發中不可或缺,用於構建動態網頁和單頁面應用。 4.JavaScript通過Node.js在後端開發中發揮作用,支持全棧開發。

C和C 在JavaScript引擎中扮演了至关重要的角色,主要用于实现解释器和JIT编译器。1)C 用于解析JavaScript源码并生成抽象语法树。2)C 负责生成和执行字节码。3)C 实现JIT编译器,在运行时优化和编译热点代码,显著提高JavaScript的执行效率。

JavaScript在現實世界中的應用包括前端和後端開發。 1)通過構建TODO列表應用展示前端應用,涉及DOM操作和事件處理。 2)通過Node.js和Express構建RESTfulAPI展示後端應用。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具