我如何努力将台式机/笔记本电脑上的固定侧边栏菜单切换到手机/平板电脑上的汉堡菜单

尽管我很清楚自己想要实现什么目标,但我真的很难让它发挥作用。我想要实现的是我的网站的响应式布局,其中侧边栏菜单固定在台式机和笔记本电脑上,但随后切换到移动设备和平板电脑的汉堡菜单?


<!DOCTYPE html>

<html>

<head>

<meta name="viewport" content="width=device-width, initial-scale=1">

<style>

body {

  font-family: "Lato", sans-serif;

}


.sidebar {

  height: 100%;

  width: 0;

  position: fixed;

  z-index: 1;

  top: 0;

  left: 0;

  background-color: #111;

  overflow-x: hidden;

  transition: 0.5s;

  padding-top: 60px;

}


.sidebar a {

  padding: 9px 8px 8px 32px;

  text-decoration: none;

  font-size: 25px;

  color: #818181;

  display: block;

  transition: 0.3s;

}


.sidebar a:hover {

  color: #f1f1f1;

}


.sidebar .closebtn {

  position: absolute;

  top: 0;

  right: 25px;

  font-size: 36px;

  margin-left: 50px;

}


.openbtn {

  font-size: 20px;

  cursor: pointer;

  background-color: #111;

  color: white;

  padding: 10px 15px;

  border: none;

}


.openbtn:hover {

  background-color: #444;

}


#main {

  transition: margin-left .5s;

  padding: 16px;

}


/* On smaller screens, where height is less than 450px, change the style of the sidenav (less padding and a smaller font size) */

@media screen and (max-height: 450px) {

  .sidebar {padding-top: 15px;}

  .sidebar a {font-size: 18px;}

}

</style>

</head>

<body>


<div id="mySidebar" class="sidebar">

  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>

  <a href="#">About</a>

  <a href="#">Services</a>

  <a href="#">Clients</a>

  <a href="#">Contact</a>

</div>


<div id="main">

  <button class="openbtn" onclick="openNav()">☰ Open Sidebar</button>  

  <h2>Collapsed Sidebar</h2>

  <p>Click on the hamburger menu/bar icon to open the sidebar, and push this content to the right.</p>

</div>


<script>

function openNav() {

  document.getElementById("mySidebar").style.width = "250px";

  document.getElementById("main").style.marginLeft = "250px";

}


function closeNav() {

  document.getElementById("mySidebar").style.width = "0";

  document.getElementById("main").style.marginLeft= "0";

}

</script>

   

</body>

</html> 


湖上湖
浏览 69回答 1
1回答

翻翻过去那场雪

您只需要在通过 JS 切换菜单时设置要更改的值(width、等),然后将它们更改为您最初使用该菜单时使用的值。此外,您可能想要隐藏和显示切换按钮。margin-leftmedia query这是一种方法:body {&nbsp; font-family: "Lato", sans-serif;}.sidebar {&nbsp; height: 100%;&nbsp; width: 250px;&nbsp; position: fixed;&nbsp; z-index: 1;&nbsp; top: 0;&nbsp; left: 0;&nbsp; background-color: #111;&nbsp; overflow-x: hidden;&nbsp; transition: 0.5s;&nbsp; padding-top: 60px;}.sidebar a {&nbsp; padding: 9px 8px 8px 32px;&nbsp; text-decoration: none;&nbsp; font-size: 25px;&nbsp; color: #818181;&nbsp; display: block;&nbsp; transition: 0.3s;}.sidebar a:hover {&nbsp; color: #f1f1f1;}.sidebar .closebtn {&nbsp; position: absolute;&nbsp; top: 0;&nbsp; right: 25px;&nbsp; font-size: 36px;&nbsp; margin-left: 50px;&nbsp; display: none;}.openbtn {&nbsp; font-size: 20px;&nbsp; cursor: pointer;&nbsp; background-color: #111;&nbsp; color: white;&nbsp; padding: 10px 15px;&nbsp; border: none;&nbsp; display: none;}.openbtn:hover {&nbsp; background-color: #444;}#main {&nbsp; transition: margin-left .5s;&nbsp; padding: 16px;&nbsp; margin-left: 250px;}/* On smaller screens, where height is less than 450px, change the style of the sidenav (less padding and a smaller font size) */@media screen and (max-height: 450px) {&nbsp; .sidebar {&nbsp; &nbsp; padding-top: 15px;&nbsp; &nbsp; width: 0;&nbsp; }&nbsp; .sidebar a {&nbsp; &nbsp; font-size: 18px;&nbsp; }&nbsp; .openbtn {&nbsp; &nbsp; display: block;&nbsp; }&nbsp; .sidebar .closebtn {&nbsp; &nbsp; display: block;&nbsp; }&nbsp; #main {&nbsp; &nbsp; margin-left: 0;&nbsp; }}<!DOCTYPE html><html><head>&nbsp; <meta name="viewport" content="width=device-width, initial-scale=1"></head><body>&nbsp; <div id="mySidebar" class="sidebar">&nbsp; &nbsp; <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>&nbsp; &nbsp; <a href="#">About</a>&nbsp; &nbsp; <a href="#">Services</a>&nbsp; &nbsp; <a href="#">Clients</a>&nbsp; &nbsp; <a href="#">Contact</a>&nbsp; </div>&nbsp; <div id="main">&nbsp; &nbsp; <button class="openbtn" onclick="openNav()">☰ Open Sidebar</button>&nbsp; &nbsp; <h2>Collapsed Sidebar</h2>&nbsp; &nbsp; <p>Click on the hamburger menu/bar icon to open the sidebar, and push this content to the right.</p>&nbsp; </div>&nbsp; <script>&nbsp; &nbsp; function openNav() {&nbsp; &nbsp; &nbsp; document.getElementById("mySidebar").style.width = "250px";&nbsp; &nbsp; &nbsp; document.getElementById("main").style.marginLeft = "250px";&nbsp; &nbsp; }&nbsp; &nbsp; function closeNav() {&nbsp; &nbsp; &nbsp; document.getElementById("mySidebar").style.width = "0";&nbsp; &nbsp; &nbsp; document.getElementById("main").style.marginLeft = "0";&nbsp; &nbsp; }&nbsp; </script></body></html>小提琴: https:&nbsp;//jsfiddle.net/1czk379m/2/编辑:切换菜单后调整窗口大小时,样式之间似乎存在一些冲突。closeNav()更改了删除元素的内联样式的操作。也改为.sidebaron&nbsp;#mySidebarcss。看看更新后的小提琴
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5