html - 打开关闭sidenav相同的按钮

标签 html css

我想让这段代码响应。它在移动设备上完美运行,但是当我在桌面上单击它时,它不起作用。我想让侧边栏像 wp. 当我在移动设备上时,它会像这样显示,但是当我在桌面上单击它时,它会像 wp 仪表板一样显示。

body {
  margin: 0;
}
.sidebar-toggle {
  position: absolute;
  left: 0;
  top: 0;
  z-index: 2;
  padding: 10px;
  background: red;
  color: white;
}
#sidebar-toggle-input {
  display: none;
}
#sidebar-toggle-input+label:before {
  content: "➡️";
}
#sidebar-toggle-input:checked + label:before {
  content: "❌"
}
.sidebar {
  position: fixed;
  width: 200px;
  transition: 0.3s;
  background: blue;
  height: 100vh;
  top: 0;
  left: 0;
  transform: translateX(-100%);
}
#sidebar-toggle-input:checked ~ .sidebar {
  transform: none;
}
<input type="checkbox" id="sidebar-toggle-input" />
<label class="sidebar-toggle" for="sidebar-toggle-input"></label>
<div class="sidebar"></div>

Here is a Codepen of my project

最佳答案

对不起,我想我没听懂你的问题,但我想这就是你想要的 所以在这里我添加了两种不同类型的导航并使用媒体查询我更改了它们中的每一个的显示

body {
  margin: 0;
  background-color: #ddd;
}
.sidebar-toggle {
  position: absolute;
  left: 0;
  top: 0;
  z-index: 2;
  padding: 10px;
  background: red;
  color: white;
}
#sidebar-toggle-input {
  display: none;
}
#sidebar-toggle-input+label:before {
  content: "➡️";
}
#sidebar-toggle-input:checked + label:before {
  content: "❌"
}
.sidebar {
  position: fixed;
  width: 200px;
  transition: 0.3s;
  background: blue;
  height: 100vh;
  top: 0;
  left: 0;
  transform: translateX(-100%);
}
#sidebar-toggle-input:checked ~ .sidebar {
  transform: none;
}
.sidebar ul{
  padding: 0;
  margin: 0;
  color: #fff;
  margin-top: 50px;
  text-align: center;
}
.sidebar ul li{
  margin-top: 10px;
  cursor: pointer;
  font-size: 20px;
  display: inline;
}

@media only screen and (min-width: 700px){
  .sidebar-toggle-input, .sidebar-toggle{
    display: none;
  }
  .sidebar{
    width: 100px;
    transform: translatex(0);
  }
  .sidebar ul div li{
    display: none;
  }
  .sidebar ul div span{
    font-size: 2rem;
  }
}
@media only screen and (min-width: 900px){
.sidebar{
    width: 300px;
  }
  .sidebar ul div li{
    display: block;
  }
}
<input type="checkbox" id="sidebar-toggle-input" />
<label class="sidebar-toggle" for="sidebar-toggle-input"></label>
<div class="sidebar">
  <ul>
    <div><span>&#9731;</span><li>home</li></div>
    <div><span>&#9731;</span><li>about</li></div>
    <div><span>&#9731;</span><li>contact</li></div>
  </ul>
</div>

关于html - 打开关闭sidenav相同的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57339139/

相关文章:

javascript - 刷新父级后,JS window.close 弹出窗口不起作用

php - 无法仅选择包含数据的列

javascript - 外部 CSS 文件未加载到 HTML 文件中

python - Flask 不加载 CSS 文件..?

javascript - 在 html 页面中滑动下一个和上一个按钮

javascript - 显示 :block not working on option tag

css align div right with at table at 100% on right

Javascript、Canvas 和调整大小

html - 如何制作 Sublime Text 3 默认模板

css - 我的菜单与浏览器窗口而不是内容对齐