javascript - 单击下拉菜单时,侧边栏需要变高

标签 javascript html css drop-down-menu

目前我有一个带有下拉功能的侧边栏。单击下拉菜单时,我希望侧边栏的高度增加。在 JavaScript 中,我有一个循环来切换按钮,以便它们可以显示/隐藏。在 CSS 中,我定义了 60% 的高度,当单击下拉菜单时,理想情况下会达到 80%。在 HTML 中,我只有 sidenav 的链接。

var dropdown = document.getElementsByClassName("dropdown-btn");
var i;

for (i = 0; i < dropdown.length; i++) {
  dropdown[i].addEventListener("click", function() {
  this.classList.toggle("active");
  var dropdownContent = this.nextElementSibling;
  if (dropdownContent.style.display === "block") {
  dropdownContent.style.display = "none";
  } else {
  dropdownContent.style.display = "block";
  }
  });
}
/* Fixed sidenav, full height */
.sidenav {
  height: 60%;
  width: 200px;
  position: fixed;
  z-index: 1;
  top: 0;
  left: 0;
  background-color: #111;
  overflow-x: hidden;
  padding-top: 20px;
}

/* Style the sidenav links and the dropdown button */
.sidenav a, .dropdown-btn {
  padding: 6px 8px 6px 16px;
  text-decoration: none;
  font-size: 20px;
  color: #818181;
  display: block;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
  cursor: pointer;
  outline: none;
}

/* On mouse-over */
.sidenav a:hover, .dropdown-btn:hover {
  color: #f1f1f1;
}

/* Main content */
.main {
  margin-left: 200px; /* Same as the width of the sidenav */
  font-size: 20px; /* Increased text to enable scrolling */
  padding: 0px 10px;
}

/* Add an active class to the active dropdown button */
.active {
  background-color: green;
  color: white;
}

/* Dropdown container (hidden by default). Optional: add a lighter background color and some left padding to change the design of the dropdown content */
.dropdown-container {
  display: none;
  background-color: #262626;
  padding-left: 8px;
}

/* Optional: Style the caret down icon */
.fa-caret-down {
  float: right;
  padding-right: 8px;
}
<div class="sidenav">
  <a href="#about">About</a>
  <a href="#services">Services</a>
  <a href="#clients">Clients</a>
  <a href="#contact">Contact</a>
  <button class="dropdown-btn">Dropdown 
    <i class="fa fa-caret-down"></i>
  </button>
  <div class="dropdown-container">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
  <a href="#contact">Search</a>
</div>

最佳答案

也许我误解了你的意思。但是,如果您只是想让 sidenav 元素的高度在下拉列表打开时达到 80%,您可以只选择该元素并使用 style 属性设置高度:

var dropdown = document.getElementsByClassName("dropdown-btn");
// Select sidenav element
var sidenav = document.getElementsByClassName("sidenav")[0];
var i;

for (i = 0; i < dropdown.length; i++) {
  dropdown[i].addEventListener("click", function() {
    this.classList.toggle("active");
    var dropdownContent = this.nextElementSibling;
    if (dropdownContent.style.display === "block") {
      dropdownContent.style.display = "none";
      // Dropdown closed, sidenav height is 60%
      sidenav.style.height = "60%";
    } else {
      dropdownContent.style.display = "block";
      // Dropdown open, sidenav height is 80%
      sidenav.style.height = "80%";
    }
  });
}
/* Fixed sidenav, full height */

.sidenav {
  height: 60%;
  width: 200px;
  position: fixed;
  z-index: 1;
  top: 0;
  left: 0;
  background-color: #111;
  overflow-x: hidden;
  padding-top: 20px;
}

/* Style the sidenav links and the dropdown button */

.sidenav a,
.dropdown-btn {
  padding: 6px 8px 6px 16px;
  text-decoration: none;
  font-size: 20px;
  color: #818181;
  display: block;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
  cursor: pointer;
  outline: none;
}

/* On mouse-over */

.sidenav a:hover,
.dropdown-btn:hover {
  color: #f1f1f1;
}

/* Main content */

.main {
  margin-left: 200px;
  /* Same as the width of the sidenav */
  font-size: 20px;
  /* Increased text to enable scrolling */
  padding: 0px 10px;
}

/* Add an active class to the active dropdown button */

.active {
  background-color: green;
  color: white;
}

/* Dropdown container (hidden by default). Optional: add a lighter background color and some left padding to change the design of the dropdown content */

.dropdown-container {
  display: none;
  background-color: #262626;
  padding-left: 8px;
}

/* Optional: Style the caret down icon */

.fa-caret-down {
  float: right;
  padding-right: 8px;
}
<div class="sidenav">
  <a href="#about">About</a>
  <a href="#services">Services</a>
  <a href="#clients">Clients</a>
  <a href="#contact">Contact</a>
  <button class="dropdown-btn">Dropdown 
    <i class="fa fa-caret-down"></i>
  </button>
  <div class="dropdown-container">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
  <a href="#contact">Search</a>
</div>

关于javascript - 单击下拉菜单时,侧边栏需要变高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57033323/

相关文章:

javascript - 通过 id name 检查元素是否有子节点

html - CSS 选择器(我认为)- 按钮不进入 Div 元素?

php - 网站适合平板电脑的屏幕,视口(viewport)不工作?

javascript - 使用 Highcharts 获取 Solid Guage 的刻度位置和标签

javascript - 如何在 Jenkins 中设置和运行 Angular JS Protractor 测试?

javascript - MODULE_NOT_FOUND 用于自适应卡片模板

javascript - 使用基于 Codeigniter 的 Jquery Java 刷新 html 表

php - 测试不安全的结帐页面

javascript - Fabricjs loadFromJSON 性能

javascript - 输入类型文件上方不需要的空间