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

ech*_*cho 3 html css css3

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

我正在使用复选框作为我的按钮,如下所示:

<input type="checkbox" id="toggle-right">
<input type="checkbox" id="toggle-left">
Run Code Online (Sandbox Code Playgroud)

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

如果我像这样颠倒顺序:

<input type="checkbox" id="toggle-left">
<input type="checkbox" id="toggle-right">
Run Code Online (Sandbox Code Playgroud)

切换左侧将起作用,但不能切换到右侧.

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

如果有人有时间看一看并帮我解决这个问题,我将不胜感激.

在查看JSFiddle时,反转复选框的顺序切换以查看我正在谈论的内容.

eit*_*hed 8

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

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; }
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)