调整屏幕大小时,boostrap侧边栏按钮不可见

时间:2017-03-16 05:51:13

标签: html css asp.net-mvc twitter-bootstrap media-queries

我想在我的asp.net MVC应用程序中使用boostrap侧边栏,我在侧边栏侧面使用一个按钮(#menu-toggle)来切换侧边栏.everthing很好但是当我调整屏幕大小时按钮消失了。我希望按钮在所有屏幕上都可见。通常我将左侧按钮设置为与侧边栏的宽度相匹配。

HTML

<div id="wrapper" class="toggled">
  <div class="container-fluid">
    <!-- Sidebar -->
    <div id="sidebar-wrapper">
      <ul class="sidebar-nav">
        <li class="sidebar-brand">
        </li>
        @Html.Action("Reports", "Dashboard")
      </ul>
    </div>
    <!-- /#sidebar-wrapper -->
    <!-- Page Content -->
    <div id="page-content-wrapper">
      <div class="container-fluid">
        <div class="row">
          <div class="col-lg-12">
            <div id="body">@RenderBody()</div>
          </div>
        </div>
      </div>
    </div>
    <!-- /#page-content-wrapper -->
  </div>
</div>

CSS

#menu-toggle {
  z-index: 5;
  position: absolute;
  top: 500px;
  left: 10px;
}

#menu-toggle.toggled {
  z-index: 5;
  position: absolute;
  top: 500px;
  left: 260px;
}

#wrapper {
  padding-left: 0;
}

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

#sidebar-wrapper {
  position: fixed;
  left: 250px;
  z-index: 1000;
  overflow-y: auto;
  margin-left: -250px;
  width: 0;
  height: 100%;
  background: #000;
}

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

#page-content-wrapper {
  padding: 15px;
  width: 100%;
}

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

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

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

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

.sidebar-nav li a:hover {
  background: rgba(255, 255, 255, 0.2);
  color: #fff;
  text-decoration: none;
}

.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 {
  background: none;
  color: #fff;
}

@@media (min-width: 768px) {
  #menu-toggle {
    display:block;
    z-index: 5;
    position: absolute;
    top: 500px;
    left: 10px;
  }
  #menu-toggle.toggled {
    z-index: 5;
    position: absolute;
    top: 500px;
    left: 260px;
  }
  #wrapper {
    padding-left: 250px;
  }
  #wrapper.toggled {
    padding-left: 0px;
  }
  #sidebar-wrapper {
    width: 250px;
  }
  #wrapper.toggled #sidebar-wrapper {
    width: 0;
  }
  #page-content-wrapper {
    padding: 20px;
  }
  #wrapper.toggled #page-content-wrapper {
    position: relative;
    margin-right: 0;
  }
}

@@media (max-width: 768px) {
  #menu-toggle {
    display:block;
    z-index: 5;
    position: absolute;
    top: 500px;
    left: 10px;
  }
  #menu-toggle.toggled {
    z-index: 5;
    position: absolute;
    top: 500px;
    left: 260px;
  }
  #wrapper {
    padding-left: 250px;
  }
  #wrapper.toggled {
    padding-left: 0px;
  }
  #sidebar-wrapper {
    width: 250px;
  }
  #wrapper.toggled #sidebar-wrapper {
    width: 0;
  }
  #page-content-wrapper {
    padding: 20px;
  }
  #wrapper.toggled #page-content-wrapper {
    position: relative;
    margin-right: 0;
  }
}

jquery

$(document).on("click", "#menu-toggle", function (e) {
    e.preventDefault();
    $(this).toggleClass("toggled");
    $("#wrapper").toggleClass("toggled");
});

1 个答案:

答案 0 :(得分:0)

使用@media代替@@media,您可以在top

中使用#menu-toggle的百分比值

&#13;
&#13;
$(document).on("click", "#menu-toggle", function(e) {
  e.preventDefault();
  $(this).toggleClass("toggled");
  $("#wrapper").toggleClass("toggled");
});
&#13;
#menu-toggle {
  z-index: 5;
  position: absolute;
  top: 500px;
  left: 10px;
}

#menu-toggle.toggled {
  z-index: 5;
  position: absolute;
  top: 50%;
  left: 260px;
}

#wrapper {
  padding-left: 0;
}

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

#sidebar-wrapper {
  position: fixed;
  left: 250px;
  z-index: 1000;
  overflow-y: auto;
  margin-left: -250px;
  width: 0;
  height: 100%;
  background: #000;
}

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

#page-content-wrapper {
  padding: 15px;
  width: 100%;
}

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

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

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

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

.sidebar-nav li a:hover {
  background: rgba(255, 255, 255, 0.2);
  color: #fff;
  text-decoration: none;
}

.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 {
  background: none;
  color: #fff;
}

@media (min-width: 768px) {
  #menu-toggle {
    display:block;
    z-index: 5;
    position: absolute;
    top: 50%;
    left: 10px;
  }
  #menu-toggle.toggled {
    z-index: 5;
    position: absolute;
    top: 500px;
    left: 260px;
  }
  #wrapper {
    padding-left: 250px;
  }
  #wrapper.toggled {
    padding-left: 0px;
  }
  #sidebar-wrapper {
    width: 250px;
  }
  #wrapper.toggled #sidebar-wrapper {
    width: 0;
  }
  #page-content-wrapper {
    padding: 20px;
  }
  #wrapper.toggled #page-content-wrapper {
    position: relative;
    margin-right: 0;
  }
}

@media (max-width: 768px) {
  #menu-toggle {
    display:block;
    z-index: 5;
    position: absolute;
    top: 50%;
    left: 10px;
  }
  #menu-toggle.toggled {
    z-index: 5;
    position: absolute;
    top: 50%;
    left: 260px;
  }
  #wrapper {
    padding-left: 250px;
  }
  #wrapper.toggled {
    padding-left: 0px;
  }
  #sidebar-wrapper {
    width: 250px;
  }
  #wrapper.toggled #sidebar-wrapper {
    width: 0;
  }
  #page-content-wrapper {
    padding: 20px;
  }
  #wrapper.toggled #page-content-wrapper {
    position: relative;
    margin-right: 0;
  }
}
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="navbar-brand" href="#menu-toggle" style="background-color:gray" id="menu-toggle">></a>
<div id="wrapper" class="toggled">
  <div class="container-fluid">
    <!-- Sidebar -->
    <div id="sidebar-wrapper">
      <ul class="sidebar-nav">
        <li class="sidebar-brand">
        </li>
        @Html.Action("Reports", "Dashboard")
      </ul>
    </div>
    <!-- /#sidebar-wrapper -->
    <!-- Page Content -->
    <div id="page-content-wrapper">
      <div class="container-fluid">
        <div class="row">
          <div class="col-lg-12">
            <div id="body">@RenderBody()</div>
          </div>
        </div>
      </div>
    </div>
    <!-- /#page-content-wrapper -->
  </div>
</div>
&#13;
&#13;
&#13;