显示侧栏菜单打开页面加载时的默认设置,并在单击角度4中的切换按钮时关闭

时间:2018-11-26 06:26:33

标签: jquery html css angular

我试图显示侧栏菜单在页面加载事件中打开,并在用户单击切换按钮时关闭。请在我的组件,css和html代码下面找到。我正在使用Angular.how在页面加载事件中显示默认的侧边栏菜单,并在切换按钮单击事件上隐藏和显示侧边栏菜单。侧边栏菜单图标未在我的StackBlitz下显示.find链接:

https://stackblitz.com/edit/angular-rvaxeq?file=src%2Fapp%2Fapp.component.ts

<!-- DashboardComponent.ts -->

toggleMenu(event) {
    console.log(event);
    $("#wrapper").toggleClass("toggled");
  }
<!-- DashboardComponent.css -->
this is my css code which I am using for toggle sidebar menu.


   

#wrapper {
  padding-left: 0;
  -webkit-transition: all 0.5s ease;
  -moz-transition: all 0.5s ease;
  -o-transition: all 0.5s ease;
  transition: all 0.5s ease;
}

#wrapper.toggled {
  padding-left: 250px;
}

#sidebar-wrapper {
  z-index: 1000;
  position: fixed;
  left: 250px;
  width: 0;
  height: 100%;
  margin-left: -250px;
  overflow-y: auto;
  background: #34383e;
  -webkit-transition: all 0.5s ease;
  -moz-transition: all 0.5s ease;
  -o-transition: all 0.5s ease;
  transition: all 0.5s ease;
}

#wrapper.toggled #sidebar-wrapper {
  width: 250px;
}

#page-content-wrapper {
  width: 100%;
  position: absolute;
  /*padding: 15px;*/
}

#wrapper.toggled #page-content-wrapper {
  position: absolute;
  margin-right: -250px;
}


/* Sidebar Styles */

.sidebar-nav {
  position: absolute;
  top: 0;
  width: 250px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.sidebar-nav li {
  text-indent: 20px;
  line-height: 40px;
}

.sidebar-nav li a {
  display: block;
  text-decoration: none;
  color: #999999;
}

  .sidebar-nav li a:hover {
    text-decoration: none;
    color: #fff;
    background: #4c5b6b;
  }

.sidebar-nav li a:active, .sidebar-nav li a:focus {
  text-decoration: none;
}

.sidebar-nav>.sidebar-brand {
  height: 65px;
  font-size: 18px;
  line-height: 60px;
}

.sidebar-nav>.sidebar-brand a {
  color: #999999;
}

.sidebar-nav>.sidebar-brand a:hover {
  color: #fff;
  background: none;
}

@media(min-width:768px) {
  #wrapper {
    padding-left: 0;
  }
  #wrapper.toggled {
    padding-left: 250px;
  }
  #sidebar-wrapper {
    width: 0;
  }
  #wrapper.toggled #sidebar-wrapper {
    width: 250px;
  }
  #page-content-wrapper {
    /*padding: 20px;*/
    position: relative;
  }
  #wrapper.toggled #page-content-wrapper {
    position: relative;
    margin-right: 0;
  }
  
}
#menu-toggle {
  font-size: 20px;
  color: #ffffff;
  font-weight: 700;
  height: 36px;
  line-height: 36px;
  display: table-cell;
  vertical-align: middle;
}

   
<!-- DashboardComponent.html-->
this is my html code for sidebar menu.

<div id="wrapper">

  <!-- Sidebar -->
  <div id="sidebar-wrapper">
    <ul class="sidebar-nav">
      
      <li>
        <a href="#"><i class="fa fa-cog"></i>Administration</a>
      </li>
    </ul>
  </div>
  <!-- /#sidebar-wrapper -->
  <!-- Page Content -->
  <div id="page-content-wrapper">
    <div class="container-fluid px-0">
      <div class="sidebar-submenu">
    <i id="menu-toggle" (click)="toggleMenu($event)" class="fas fa-bars"></i>
    
  </div>
      
     
        
      </div>
      
    </div>
  </div>
  

1 个答案:

答案 0 :(得分:0)

使用[ngClass]="{'toggled': show==true}"

See working code

HTML

<div id="wrapper" [ngClass]="{'toggled': show==true}">

TS

show:boolean=true;
//onclick
toggleMenu() {
this.show=this.show==false?true:false;
  }

使用内容页面进行编辑

See code