我想让这段代码响应。它在移动设备上完美运行,但是当我在桌面上单击它时,它不起作用。我想让侧边栏像 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>
最佳答案
对不起,我想我没听懂你的问题,但我想这就是你想要的 所以在这里我添加了两种不同类型的导航并使用媒体查询我更改了它们中的每一个的显示
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>☃</span><li>home</li></div>
<div><span>☃</span><li>about</li></div>
<div><span>☃</span><li>contact</li></div>
</ul>
</div>
关于html - 打开关闭sidenav相同的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57339139/