Search code examples
csshovernavbarhide

OnClick hide navbar


I have this navbar and it works fine on computer screen, because of :hover. But on mobile screen you have to click on navbar to open it. I want to hide navbar when li is selected. But I'm not sure how to do it :/ I have to make main-menu width 60px and overflow hidden. I made it with javasript but it works only once. If you want to open it again, the width and overwflow ofc stays the same.

.fa {
  position: relative;
  display: table-cell;
  width: 60px;
  height: 36px;
  text-align: center;
  vertical-align: middle;
  font-size:20px;
}

.stick{
    position: sticky;
    top: 0;
    bottom: 0;
}

.main-menu:hover,nav.main-menu.expanded {
    width:250px;
    overflow:visible;
}

li:hover + .main-menu:hover, nav.main-menu.expanded{
    overflow: hidden !important;
    width: 60px !important;
}

.main-menu {
    background: grey;
    border-right:1px solid #e5e5e5;
    height:100%;
    width:60px;
    -webkit-transition:width .05s linear;
    transition:width .05s linear;
    -webkit-transform:translateZ(0) scale(1,1);
    z-index:1000;
    position: absolute;
}

.main-menu>ul {
    margin:7px 0;
    overflow: hidden;
}

.main-menu li {
    position:relative;
    display:block;
    width:250px;
}

.main-menu li>a {
    position:relative;
    display:table;
    border-collapse:collapse;
    border-spacing:0;
    color:#999;
     font-family: arial;
    font-size: 14px;
    text-decoration:none;
    -webkit-transform:translateZ(0) scale(1,1);
    -webkit-transition:all .1s linear;
    transition:all .1s linear;
}

.main-menu .nav-icon {
    position:relative;
    display:table-cell;
    width:60px;
    height:36px;
    text-align:center;
    vertical-align:middle;
    font-size:18px;
}

.main-menu .nav-text {
  position:relative;
  display:table-cell;
  vertical-align:middle;
  width:190px;
}

.no-touch .scrollable.hover {
  overflow-y:hidden;
}

.no-touch .scrollable.hover:hover {
  overflow-y:auto;
  overflow:visible;
}

a:hover,a:focus {
  text-decoration:none;
}

nav {
  -webkit-user-select:none;
  -moz-user-select:none;
  -ms-user-select:none;
  -o-user-select:none;
  user-select:none;
}

nav ul,nav li {
  outline:0;
  margin:0;
  padding:0;
}
.main-menu li:hover>a,nav.main-menu li.active>a,.dropdown-menu>li>a:hover,.dropdown-menu>li>a:focus,.dropdown-menu>.active>a,.dropdown-menu>.active>a:hover,.dropdown-menu>.active>a:focus,.no-touch .dashboard-page nav.dashboard-menu ul li:hover a,.dashboard-page nav.dashboard-menu ul li.active a {
  color:black;
  background-color:#5fa2db;
}

.area {
    height: 100%;
}

@font-face {
  font-family: 'Titillium Web';
  font-style: normal;
  font-weight: 300;
  src: local('Titillium WebLight'), local('TitilliumWeb-Light'), url(http://themes.googleusercontent.com/static/fonts/titilliumweb/v2/anMUvcNT0H1YN4FII8wpr24bNCNEoFTpS2BTjF6FB5E.woff) format('woff');
}

.container{
    margin: 0 !important;
}
   <nav class="main-menu">
            <ul class="stick">
                <li>
                    <a href="#1">
                        <i class="fa fa-home fa-2x"></i>
                        <span class="nav-text">
                            aa
                        </span>
                    </a>
                  
                </li>
                <li class="has-subnav">
                    <a href="#">
                        <i class="fa fa-laptop fa-2x"></i>
                        <span class="nav-text">
                           bb
                        </span>
                    </a>
                    
                </li>
                <li class="has-subnav">
                    <a href="#">
                       <i class="fa fa-list fa-2x"></i>
                        <span class="nav-text">
                            cc
                        </span>
                    </a>
                    
                </li>
                <li class="has-subnav">
                    <a href="#">
                       <i class="fa fa-folder-open fa-2x"></i>
                        <span class="nav-text">
                            dd
                        </span>
                    </a>
                   
                </li>
                <li>
                    <a href="#">
                        <i class="fa fa-bar-chart-o fa-2x"></i>
                        <span class="nav-text">            
                            ee
                        </span>
                    </a>
                </li>
             
            </ul>
        </nav>


Solution

  • document.getElementsByTagName("body")[0].onclick = function(event){
      if(event.target.offsetParent.offsetParent.tagName == "LI"){
        document.getElementsByClassName("main-menu")[0].style.width = "60px";
      }
    }
    document.getElementsByClassName("main-menu")[0].onmouseover = function(event){
      this.style.width = "250px";
      this.style.overflow = "visible";
    }