国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁(yè) web前端 js教程 帶有圖示的 Angular 可展開(kāi)和可折疊側(cè)邊欄的分步指南

帶有圖示的 Angular 可展開(kāi)和可折疊側(cè)邊欄的分步指南

Nov 26, 2024 am 04:30 AM

使用圖示建立 Angular 可展開(kāi)和可折疊側(cè)邊欄

Step-by-Step Guide to an Angular Expandable and Collapsible Sidebar with Icons

Step-by-Step Guide to an Angular Expandable and Collapsible Sidebar with Icons

在 Angular 中建立可擴(kuò)展和可折疊的側(cè)邊欄可以顯著增強(qiáng)應(yīng)用程式的使用者體驗(yàn)。本教程提供了建立此類側(cè)邊欄的逐步指南,其中包含圖示和平滑過(guò)渡。我們將涵蓋從設(shè)定元件結(jié)構(gòu)到應(yīng)用程式樣式和邏輯來(lái)切換側(cè)邊欄的所有內(nèi)容。


為什麼要使用可折疊側(cè)邊欄?

可折疊側(cè)邊欄透過(guò)以下方式提高應(yīng)用程式的可用性:

  • 節(jié)省螢?zāi)豢臻g。
  • 提供輕鬆的導(dǎo)航。
  • 保持介面整潔有序。

建立側(cè)邊欄的分步指南

1. 設(shè)定你的 Angular 項(xiàng)目

首先,請(qǐng)確保您安裝了 Angular CLI。如果沒(méi)有,請(qǐng)執(zhí)行:

npm install -g @angular/cli

建立一個(gè)新的 Angular 專案:

ng new angular-sidebar
cd angular-sidebar

產(chǎn)生必要的組件:

ng generate component sidebar

2. 定義側(cè)邊欄結(jié)構(gòu)

app.component.html

這將作為應(yīng)用程式的主容器。新增側(cè)邊欄和用於切換其狀態(tài)的按鈕:

<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. 實(shí)作側(cè)邊欄組件

sidebar.component.html

定義側(cè)邊欄的 HTML 結(jié)構(gòu),包括帶有巢狀項(xiàng)目的選單:

<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;
    }
  }
}

4. 設(shè)定側(cè)邊欄的樣式

app.component.scss

新增版面配置和轉(zhuǎn)場(chǎng)的全域樣式:

.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

定義側(cè)邊欄和選單的樣式:

.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. 運(yùn)行應(yīng)用程式

啟動(dòng)開(kāi)發(fā)伺服器:

ng serve

導(dǎo)覽至 http://localhost:4200/ 以查看正在執(zhí)行的側(cè)邊欄。


常見(jiàn)問(wèn)題解答

如何自訂側(cè)邊欄圖示?

修改 sidebar.component.ts 中的 menuItems 陣列並提供適當(dāng)?shù)膱D示類別。

可以加入選單展開(kāi)動(dòng)畫(huà)嗎?

是的,使用 Angular 的動(dòng)畫(huà)模組在選單開(kāi)啟和關(guān)閉時(shí)添加平滑的過(guò)渡。

如何調(diào)整側(cè)邊欄寬度?

更新 sidebar.component.scss 中展開(kāi)和折疊狀態(tài)的寬度屬性。


本指南涵蓋了在 Angular 中建立功能性可擴(kuò)充和可折疊側(cè)邊欄的所有基本步驟。您可以進(jìn)一步客製化設(shè)計(jì)和功能以滿足您的應(yīng)用需求。

以上是帶有圖示的 Angular 可展開(kāi)和可折疊側(cè)邊欄的分步指南的詳細(xì)內(nèi)容。更多資訊請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願(yuàn)投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請(qǐng)聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

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

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強(qiáng)大的PHP整合開(kāi)發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺(jué)化網(wǎng)頁(yè)開(kāi)發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)程式碼編輯軟體(SublimeText3)

熱門(mén)話題

在JavaScript中使用哪些評(píng)論符號(hào):一個(gè)明確的解釋 在JavaScript中使用哪些評(píng)論符號(hào):一個(gè)明確的解釋 Jun 12, 2025 am 10:27 AM

在JavaScript中,選擇單行註釋(//)還是多行註釋(//)取決於註釋的目的和項(xiàng)目需求:1.使用單行註釋進(jìn)行快速、內(nèi)聯(lián)的解釋;2.使用多行註釋進(jìn)行詳細(xì)的文檔說(shuō)明;3.保持註釋風(fēng)格的一致性;4.避免過(guò)度註釋;5.確保註釋與代碼同步更新。選擇合適的註釋風(fēng)格有助於提高代碼的可讀性和可維護(hù)性。

JavaScript評(píng)論的最終指南:增強(qiáng)代碼清晰度 JavaScript評(píng)論的最終指南:增強(qiáng)代碼清晰度 Jun 11, 2025 am 12:04 AM

是的,javascriptcommentsarenectary和shouldshouldshouldseffectional.1)他們通過(guò)codeLogicAndIntentsgudedepleders,2)asevitalincomplexprojects,和3)handhanceClaritywithOutClutteringClutteringThecode。

Java vs. JavaScript:清除混亂 Java vs. JavaScript:清除混亂 Jun 20, 2025 am 12:27 AM

Java和JavaScript是不同的編程語(yǔ)言,各自適用於不同的應(yīng)用場(chǎng)景。 Java用於大型企業(yè)和移動(dòng)應(yīng)用開(kāi)發(fā),而JavaScript主要用於網(wǎng)頁(yè)開(kāi)發(fā)。

JavaScript評(píng)論:簡(jiǎn)短說(shuō)明 JavaScript評(píng)論:簡(jiǎn)短說(shuō)明 Jun 19, 2025 am 12:40 AM

JavascriptconcommentsenceenceEncorenceEnterential gransimenting,reading and guidingCodeeXecution.1)單inecommentsareusedforquickexplanations.2)多l(xiāng)inecommentsexplaincomplexlogicorprovideDocumentation.3)

掌握J(rèn)avaScript評(píng)論:綜合指南 掌握J(rèn)avaScript評(píng)論:綜合指南 Jun 14, 2025 am 12:11 AM

評(píng)論arecrucialinjavascriptformaintainingclarityclarityandfosteringCollaboration.1)heelpindebugging,登機(jī),andOnderStandingCodeeVolution.2)使用林格forquickexexplanations andmentmentsmmentsmmentsmments andmmentsfordeffordEffordEffordEffordEffordEffordEffordEffordEddeScriptions.3)bestcractices.3)bestcracticesincracticesinclud

JavaScript數(shù)據(jù)類型:深度潛水 JavaScript數(shù)據(jù)類型:深度潛水 Jun 13, 2025 am 12:10 AM

JavaScripthasseveralprimitivedatatypes:Number,String,Boolean,Undefined,Null,Symbol,andBigInt,andnon-primitivetypeslikeObjectandArray.Understandingtheseiscrucialforwritingefficient,bug-freecode:1)Numberusesa64-bitformat,leadingtofloating-pointissuesli

JavaScript與Java:開(kāi)發(fā)人員的全面比較 JavaScript與Java:開(kāi)發(fā)人員的全面比較 Jun 20, 2025 am 12:21 AM

JavaScriptIspreferredforredforwebdevelverment,而Javaisbetterforlarge-ScalebackendsystystemsandSandAndRoidApps.1)JavascriptexcelcelsincreatingInteractiveWebexperienceswebexperienceswithitswithitsdynamicnnamicnnamicnnamicnnamicnemicnemicnemicnemicnemicnemicnemicnemicnddommanipulation.2)

JavaScript:探索用於高效編碼的數(shù)據(jù)類型 JavaScript:探索用於高效編碼的數(shù)據(jù)類型 Jun 20, 2025 am 12:46 AM

javascripthassevenfundaMentalDatatypes:數(shù)字,弦,布爾值,未定義,null,object和symbol.1)numberSeadUble-eaduble-ecisionFormat,forwidevaluerangesbutbecautious.2)

See all articles