猿问

如果展开的菜单行溢出,则滚动菜单

我有一个固定的侧面导航菜单,其中包含一个Dropdown可以展开/折叠其他行的菜单项。

如果Dropdown在容器底部展开时,如果不手动向下滚动,我将看不到菜单项:

如何使它Dropdown在该位置展开并自动向下滚动(如果需要)足以显示所有子项?:

http://img2.mukewang.com/6289f17000016f5602000192.jpghttp://img4.mukewang.com/6289f1750001cdc802130200.jpg

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";

  }

  });

}

<div class="sidenav">

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

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

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

  <a href="#clients">MenuItem</a>

  <a href="#clients">MenuItem6</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">Contact</a>

  <a href="#contact">Search</a>

  <a href="#clients">MenuItem2</a>

  <a href="#clients">MenuItem9</a>

</div>


<div class="main">

  <h2>Sidebar Dropdown</h2>

  <p>Click on the dropdown button to open the dropdown menu inside the side navigation.</p>

  <p>This sidebar is of full height (100%) and always shown.</p>

  <p>Some random text..</p>

</div>


跃然一笑
浏览 201回答 1
1回答

萧十郎

根据您的评论;this.scrollIntoView({ block: 'end' })最后一个孩子应该做的伎俩!{ block: 'end' }定义垂直对齐,使其滚动到可视元素的底部第二条评论;通过比较最后一个子元素的底部与可滚动元素的可见部分,我们可以确定该行是否可见并决定滚动。var dropdown = document.getElementsByClassName("dropdown-btn");var sidenav = document.getElementsByClassName("sidenav")[0];for (i = 0; i < dropdown.length; i++) {&nbsp; dropdown[i].addEventListener("click", function() {&nbsp;&nbsp;&nbsp; this.classList.toggle("active");&nbsp; var dropdownContent = this.nextElementSibling;&nbsp; &nbsp;&nbsp; if (dropdownContent.style.display === "block") {&nbsp; &nbsp; dropdownContent.style.display = "none";&nbsp; } else {&nbsp; &nbsp; dropdownContent.style.display = "block";&nbsp; }&nbsp;&nbsp;&nbsp; // Get last-child bottom&nbsp; const lastChild = dropdownContent.children[dropdownContent.children.length - 1];&nbsp; const lastChildRect = lastChild.getBoundingClientRect();&nbsp; const lastChildBottom = lastChildRect.bottom;&nbsp;&nbsp;&nbsp; // Get height off scrollable element&nbsp; const sidenavHeight = sidenav.clientHeight;&nbsp;&nbsp;&nbsp; // If visible&nbsp; if (lastChildBottom > sidenavHeight) {&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; // Scroll into view&nbsp; &nbsp; &nbsp; console.log('Scroll');&nbsp; &nbsp; &nbsp; lastChild.scrollIntoView({block: "end"});&nbsp; }&nbsp; });}body {&nbsp; font-family: "Lato", sans-serif;}/* Fixed sidenav, full height */.sidenav {&nbsp; height: 170px;&nbsp; width: 200px;&nbsp; position: fixed;&nbsp; z-index: 1;&nbsp; top: 0;&nbsp; left: 0;&nbsp; background-color: #111;&nbsp; overflow-x: hidden;&nbsp; padding-top: 20px;}/* Style the sidenav links and the dropdown button */.sidenav a, .dropdown-btn {&nbsp; padding: 6px 8px 6px 16px;&nbsp; text-decoration: none;&nbsp; font-size: 20px;&nbsp; color: #818181;&nbsp; display: block;&nbsp; border: none;&nbsp; background: none;&nbsp; width: 100%;&nbsp; text-align: left;&nbsp; cursor: pointer;&nbsp; outline: none;}/* On mouse-over */.sidenav a:hover, .dropdown-btn:hover {&nbsp; color: #f1f1f1;}/* Main content */.main {&nbsp; margin-left: 200px; /* Same as the width of the sidenav */&nbsp; font-size: 20px; /* Increased text to enable scrolling */&nbsp; padding: 0px 10px;}/* Add an active class to the active dropdown button */.active {&nbsp; background-color: green;&nbsp; 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 {&nbsp; display: none;&nbsp; background-color: #262626;&nbsp; padding-left: 8px;}/* Optional: Style the caret down icon */.fa-caret-down {&nbsp; float: right;&nbsp; padding-right: 8px;}/* Some media queries for responsiveness */@media screen and (max-height: 450px) {&nbsp; .sidenav {padding-top: 15px;}&nbsp; .sidenav a {font-size: 18px;}}<div class="sidenav">&nbsp; <a href="#about">About</a>&nbsp; <a href="#services">Services</a>&nbsp; <a href="#clients">Clients</a>&nbsp; <a href="#clients">MenuItem</a>&nbsp; <a href="#clients">MenuItem6</a>&nbsp; &nbsp; &nbsp; <button class="dropdown-btn">Dropdown&nbsp;&nbsp; &nbsp; <i class="fa fa-caret-down"></i>&nbsp; </button>&nbsp; <div class="dropdown-container">&nbsp; &nbsp; <a href="#">Link 1</a>&nbsp; &nbsp; <a href="#">Link 2</a>&nbsp; &nbsp; <a href="#">Link 3</a>&nbsp; </div>&nbsp; <a href="#contact">Contact</a>&nbsp; <a href="#contact">Search</a>&nbsp; <a href="#clients">MenuItem2</a>&nbsp; <a href="#clients">MenuItem9</a></div><div class="main">&nbsp; <h2>Sidebar Dropdown</h2>&nbsp; <p>Click on the dropdown button to open the dropdown menu inside the side navigation.</p>&nbsp; <p>This sidebar is of full height (100%) and always shown.</p>&nbsp; <p>Some random text..</p></div>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答