启动我自己的网站时如何隐藏切换侧边栏?

时间:2019-06-04 00:25:28

标签: javascript jquery html css

我为菜单创建了一个带有切换侧边栏的站点,但是每次加载站点时用户无需单击切换侧边栏按钮时,切换侧边栏内部的按钮都会显示出来。谢谢,请指导我解决此问题的方法。

This is when I load the site side menu will automatic popout without clicking on the toggle sidebar This is what I want when I load my site

1 个答案:

答案 0 :(得分:0)

这是一个使用html,css和5行javascript的非常简单的示例:

window.addEventListener('DOMContentLoaded', (event) => {
  const button = document.querySelector('button');

  button.addEventListener('click', _ => {
    document.getElementById('sidebar').classList.toggle('collapsed');
  });
});
html, body {
  width: 100%;
  height: 100%;
  margin: 0;
}

#sidebar, #content {
  height: 100%;
  overflow:auto;
  float:left;
  transition: width .35s;
}

#sidebar {
  background:white;
  width: 30%;
  box-shadow: 2px 0 4px rgba(0,0,0,0.5);
}

#sidebar.collapsed {
  width: 0;
}

#sidebar.collapsed + #content {
  width: 100%;
}

#content {
  background:gray;
  width: 70%;
}

button {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border:none;
  outline:none;
  background: black;
  margin: 20px;
  cursor:pointer;
}

h3 {
  font-family: sans-serif;
  color:white;
  text-align:center;
}
<div id="sidebar" class="collapsed">
  
</div>
<div id="content">
  <button title="Toggle sidebar"></button>
  <h3>Some content</p>
</div>