Angular - 侧边栏导航
Angular - Sidebar Navigation
我尝试从左侧导航创建滑入,而不是传统的 bootstrap 在折叠时向下滑动导航。
HTML 用于切换按钮
<button id="collapseBtn" class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation" (click)="toggleSideBar()" >
Toggle</button>
HTML 侧边栏
<div class="sidebar">
<ul>
<li>S-Dashboard</li>
<li>S-Voucher</li>
</ul>
</div>
CSS 侧边栏
.sidebar {
background: #1a2580;
color: white;
height: 100%;
width: 0;
position: fixed;
z-index: 1;
top: 0;
left: 0;
overflow-x: hidden;
padding-top: 60px;
transition: 0.5s;
}
.showsidebar {
width: 250px;
}
.hidesidebar {
width: 0px;
}
组件文件中的 ToggleSideBar 函数
toggleSideBar() {
document.getElementsByClassName('sidebar')[0].classList.add('showsidebar');
this.sideBarOpen = true;
}
单击切换按钮可根据需要滑出导航,但不会将内容推到右侧。结果,内容被隐藏在侧边栏下方。
如何向右推送内容?
其次,我尝试使用 HostListener 在页面上的任意位置单击时关闭边栏,但这似乎不起作用。
您可以在 body
元素上切换 class,当您切换侧边菜单时,该元素应负责移动(推送)整个页面内容。
styles.css
body {
position: relative;
overflow: hidden;
transition: left 0.5s ease; /* Animation styles are just for demo */
left:0;
}
body.push {
left:250px;
}
在您的组件中:
import { Component, OnInit, HostListener, ElementRef } from '@angular/core';
@Component({
selector: 'app-topnav',
templateUrl: './topnav.component.html',
styleUrls: ['./topnav.component.css']
})
export class TopnavComponent implements OnInit {
isMenuSmall:boolean = true;
sideBarOpen: boolean = false;
constructor(private el:ElementRef) { }
// Your initial click listener on the host element
@HostListener('click', ['$event'])onClick(event) {
event.stopPropagation();
if (event.target.id == "collapseBtn") {
document.getElementsByClassName('sidebar')[0].classList.add('showsidebar');
document.body.classList.add('push');
this.sideBarOpen = true;
} else {
if (this.sideBarOpen) {
document.getElementsByClassName('sidebar')[0].classList.remove('showsidebar');
document.body.classList.remove('push');
this.sideBarOpen = false;
}
}
}
// Click listener on the window object to handle clicks anywhere on
// the screen.
@HostListener('window:click', ['$event']) onOutsideClick(event){
if(this.sideBarOpen && !this.el.nativeElement.contains(event.target)){
this.sideBarOpen=false;
document.getElementsByClassName('sidebar')[0].classList.remove('showsidebar');
document.body.classList.remove('push');
}
}
ngOnInit() {
}
toggleSideBar() {
}
}
当然,上面的代码还可以进一步增强,但我希望它向您展示了一种实现目标的方法。
我尝试从左侧导航创建滑入,而不是传统的 bootstrap 在折叠时向下滑动导航。
HTML 用于切换按钮
<button id="collapseBtn" class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation" (click)="toggleSideBar()" >
Toggle</button>
HTML 侧边栏
<div class="sidebar">
<ul>
<li>S-Dashboard</li>
<li>S-Voucher</li>
</ul>
</div>
CSS 侧边栏
.sidebar {
background: #1a2580;
color: white;
height: 100%;
width: 0;
position: fixed;
z-index: 1;
top: 0;
left: 0;
overflow-x: hidden;
padding-top: 60px;
transition: 0.5s;
}
.showsidebar {
width: 250px;
}
.hidesidebar {
width: 0px;
}
组件文件中的 ToggleSideBar 函数
toggleSideBar() {
document.getElementsByClassName('sidebar')[0].classList.add('showsidebar');
this.sideBarOpen = true;
}
单击切换按钮可根据需要滑出导航,但不会将内容推到右侧。结果,内容被隐藏在侧边栏下方。
如何向右推送内容?
其次,我尝试使用 HostListener 在页面上的任意位置单击时关闭边栏,但这似乎不起作用。
您可以在 body
元素上切换 class,当您切换侧边菜单时,该元素应负责移动(推送)整个页面内容。
styles.css
body {
position: relative;
overflow: hidden;
transition: left 0.5s ease; /* Animation styles are just for demo */
left:0;
}
body.push {
left:250px;
}
在您的组件中:
import { Component, OnInit, HostListener, ElementRef } from '@angular/core';
@Component({
selector: 'app-topnav',
templateUrl: './topnav.component.html',
styleUrls: ['./topnav.component.css']
})
export class TopnavComponent implements OnInit {
isMenuSmall:boolean = true;
sideBarOpen: boolean = false;
constructor(private el:ElementRef) { }
// Your initial click listener on the host element
@HostListener('click', ['$event'])onClick(event) {
event.stopPropagation();
if (event.target.id == "collapseBtn") {
document.getElementsByClassName('sidebar')[0].classList.add('showsidebar');
document.body.classList.add('push');
this.sideBarOpen = true;
} else {
if (this.sideBarOpen) {
document.getElementsByClassName('sidebar')[0].classList.remove('showsidebar');
document.body.classList.remove('push');
this.sideBarOpen = false;
}
}
}
// Click listener on the window object to handle clicks anywhere on
// the screen.
@HostListener('window:click', ['$event']) onOutsideClick(event){
if(this.sideBarOpen && !this.el.nativeElement.contains(event.target)){
this.sideBarOpen=false;
document.getElementsByClassName('sidebar')[0].classList.remove('showsidebar');
document.body.classList.remove('push');
}
}
ngOnInit() {
}
toggleSideBar() {
}
}
当然,上面的代码还可以进一步增强,但我希望它向您展示了一种实现目标的方法。