要在切换关闭javascript上显示的图标

时间:2014-09-30 11:39:16

标签: javascript jquery css

我正在创建一个非画布菜单。我试图得到它,以便当关闭切换有效时,它将只显示菜单图标。

我在尝试解决这个问题时遇到了一些麻烦,所以只有当关闭画布时才会看到图标,而当画布打开时,它将能够看到整个链接。

也似乎无法将标题放在侧边栏处于侧边栏的顶部,只能在边栏处获得标题。

直播示例:http://codepen.io/riwakawebsitedesigns/pen/oxwzt

代码

<div class="wrapper">

<div class="offcanvas-header">
        <i class="fa fa-dedent fa-lg toggle"></i>
</div>

<nav class="offcanvas">
    <ul>
        <li><a href="#"><i class="fa fa-dedent fa-lg"></i> Home</a></li>
        <li><a href="#"><i class="fa fa-dedent fa-lg"></i> About</a></li>
        <li><a href="#"><i class="fa fa-dedent fa-lg"></i> Portfolio</a></li>
        <li><a href="#"><i class="fa fa-dedent fa-lg"></i> Contact me</a></li>
    </ul>
</nav>

<div id="page-wrapper">
<main>
</main>
</div>

</div>

<script type="text/javascript">
$(function() {
  var wrapper = $(".wrapper"),
      toggle = $(".toggle"),
      nav = $(".offcanvas");
  toggle.on("click", function() {
    wrapper.toggleClass("nav-open");
    // Change the font-awesome icons on click.
    toggle.toggleClass("fa fa-dedent fa-lg");
    toggle.toggleClass("fa fa-dedent fa-lg");
  });


     $(window).on("click", function(e) {
        if (
          wrapper.hasClass("nav-open") && 
          !$(e.target).parents(nav).hasClass("offcanvas") && 
          !$(e.target).hasClass("toggle")
        ) {
            wrapper.removeClass("nav-open");
          }
      });

});
</script>

CSS

/* Reset */

h1,
h2,
h3,
h4,
h5,
h6 {
    padding: 0 !important;
    margin: 0 !important;
}


body {
    margin: 0;
    padding: 0;
}

.wrapper {
    overflow-x: hidden;
    position: relative;
    left: 0;
}

.offcanvas-header {
    background: none repeat scroll 0 0 #ffffff;
   border-bottom: 1px solid #e5e5e5;
   margin: 0;
   padding: 0;
   height: 50px;
}

.offcanvas-header i {
    line-height: 50px;
    color: #6D6D6D;
    font-weight: bold;
    padding-left: 20px;
    font-size: 2rem;
}

.toggle {
    color: #eee;
    cursor: pointer;
    position: relative;
    top: 0px;
    left: 0px;
}

.offcanvas {
    background-color: #515151;
    border-right: 1px solid #515151;
    height: 100%;
   padding-top: 50px;
    transition: all 0.3s ease 0s;
    color: #fff;
    position: fixed;
    top: 0;
    left: -231px;
    width: 210px;
    height: 100%;
    padding: 10px;
}

.offcanvas ul {
    margin: 0;
    padding: 0;
}

.offcanvas ul li {
    list-style: none;
    position:relative;
    margin: 0;
    padding: 0;
}

.offcanvas ul li a {
    display: block;
    position:relative;
    padding-top: 10px;
    padding-bottom: 10px;
    border-bottom: 1px solid #585858;
   color: #c4c4c4;
   font-size: 14px;
   padding-left: 0;
    color: rgba(255, 255, 255, 0.8);
    text-decoration: none;
}

.offcanvas ul li a i {
    padding-right: 10px;
    font-size: 14px;
}

.offcanvas ul li a:hover {
    color: white;
    font-weight: bold;
}

.nav-open {
    left: 231px;
}

.nav-open .offcanvas {
  left: 0;
}

.wrapper, .offcanvas {
    -webkit-transition: left .2s ease;
    transition: left .2s ease;
}

1 个答案:

答案 0 :(得分:0)

您可以将图标浮动到右侧。并在导航打开时删除。

a .fa {
  float: right;
}

.nav-open a .fa {
  float: none;
}

Codepen Demo