离屏导航 - 右侧和左侧 - 仅限CSS

时间:2014-07-21 06:32:04

标签: html css css3

我正在尝试创建一个只有css的右侧和左侧离屏导航抽屉,我在让每一方都正常工作时遇到了问题。

我正在使用复选框作为我的按钮:

<input type="checkbox" id="toggle-right">
<input type="checkbox" id="toggle-left">

如果我在顶部有一个向右拨动的位置,那么右侧抽屉会打开,而不是左侧。

如果我颠倒这样的顺序:

<input type="checkbox" id="toggle-left">
<input type="checkbox" id="toggle-right">

向左切换可以正常工作,但不能向右切换。

我做了一个JSFiddle来告诉你我的意思。

如果有人有时间看一看并帮我解决这个问题,我会很感激。

在查看JSFiddle时,反转复选框的顺序切换以查看我在说什么。

1 个答案:

答案 0 :(得分:8)

问题在于使用+ - 相邻的兄弟选择器。由于它仅适用于复选框的下一个元素,因此它仅适用于其中一个元素。解决方案是使用~ - 一般兄弟选择器。

&#13;
&#13;
header {
  width: 100%;
  height: 90px; 
  background-color:#f1f1f1;}

.menu-toggle {
  text-decoration: none;
  text-align: center;
  width: 44px;
  height: 44px;
  font-size: 30px;
  color: rgba(0, 0, 0, 0.5);
  -webkit-transition: all 0.15s ease-out 0s;
  -moz-transition: all 0.15s ease-out 0s;
  transition: all 0.15s ease-out 0s;
  position: absolute;
  top: 0px;
  right: auto;
  bottom: 0;
  left: 20px;
  z-index: 2; }

.menu-toggle:hover {
  color: #000000; }

#toggle-left {
  display: none; }
#toggle-left:checked ~ .page-wrap nav.menu {
  left: 0px; }
#toggle-left:checked ~ .page-wrap .menu-toggle {
  left: 220px; }

.profile-toggle {
  text-decoration: none;
  text-align: center;
  width: 44px;
  height: 44px;
  font-size: 30px;
  color: rgba(0, 0, 0, 0.5);
  -webkit-transition: all 0.15s ease-out 0s;
  -moz-transition: all 0.15s ease-out 0s;
  transition: all 0.15s ease-out 0s;
  position: absolute;
  top: 0px;
  right: 40px;
  bottom: 0;
  left: auto;
  z-index: 2; }

.profile-toggle:hover {
  color: #000000; }

#toggle-right {
  display: none; }
#toggle-right:checked + .page-wrap nav.profile {
  right: 0px; }
#toggle-right:checked + .page-wrap .profile-toggle {
  right: 220px; }

nav.menu {
  position: fixed;
  top: 0px;
  right: auto;
  bottom: 0px;
  left: -270px;
  -webkit-transition: all 0.15s ease-out 0s;
  -moz-transition: all 0.15s ease-out 0s;
  transition: all 0.15s ease-out 0s;
  height: auto;
  width: 200px;
  background: #111111;
  z-index: 2000; }


nav.profile {
  position: fixed;
  top: 0px;
  right: -270px;
  bottom: 0px;
  left: auto;
  -webkit-transition: all 0.15s ease-out 0s;
  -moz-transition: all 0.15s ease-out 0s;
  transition: all 0.15s ease-out 0s;
  height: auto;
  width: 200px;
  background: #990000;
  z-index: 2000; }
&#13;
<input type="checkbox" id="toggle-left">
<input type="checkbox" id="toggle-right">

<div class="page-wrap">
  <header>
    <div class="top-bar">
      <label for="toggle-left" class="menu-toggle">☰</label>
      <label for="toggle-right" class="profile-toggle"><b>+</b></label>
    </div>
    <div class="middle-line"></div>
    <div class="bottom-bar"></div>
  </header>

  <nav class="menu">
  </nav>

  <nav class="profile">

  </nav>
&#13;
&#13;
&#13;

相关问题