为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

跃然一笑 2022-05-22 16:16:18
我有一个固定的侧面导航菜单,其中包含一个Dropdown可以展开/折叠其他行的菜单项。如果Dropdown在容器底部展开时,如果不手动向下滚动,我将看不到菜单项:如何使它Dropdown在该位置展开并自动向下滚动(如果需要)足以显示所有子项?: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>
查看完整描述

1 回答

?
萧十郎

TA贡献1815条经验 获得超13个赞

根据您的评论;


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++) {

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

  }

  

  // Get last-child bottom

  const lastChild = dropdownContent.children[dropdownContent.children.length - 1];

  const lastChildRect = lastChild.getBoundingClientRect();

  const lastChildBottom = lastChildRect.bottom;

  

  // Get height off scrollable element

  const sidenavHeight = sidenav.clientHeight;

  

  // If visible

  if (lastChildBottom > sidenavHeight) {

      

      // Scroll into view

      console.log('Scroll');

      lastChild.scrollIntoView({block: "end"});

  }

  });

}

body {

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

}


/* Fixed sidenav, full height */

.sidenav {

  height: 170px;

  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;

}


/* Some media queries for responsiveness */

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

  .sidenav {padding-top: 15px;}

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

}

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


查看完整回答
反对 回复 2022-05-22
  • 1 回答
  • 0 关注
  • 200 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号