如何使用CSS/JS滚动到特定元素

Sho*_*ate 1 html javascript css scroll css3

这是虚拟代码.我的要求是当我点击最后一个li元素时,hidden div它会显示在里面的一个元素.但我需要scroll向下看它.所以,当我点击liid=hello再往下窗口会自动滚动到该分区?

首先使用CSS然后使用JS而不使用jQuery.

var ele=document.getElementById('hello');


ele.addEventListener("click", function (event) {
    document.getElementById("hidden-div").style.display="block";
},false);
Run Code Online (Sandbox Code Playgroud)
.fixed-height-div{
 height:120px;
 overflow-y:scroll;
}

.fixed-height-div > li{
    background-color:lightblue;
    list-style-type: none;
}
Run Code Online (Sandbox Code Playgroud)
<div class="fixed-height-div">
  <li>A</li>
  <li>B</li>
  <li>C</li>
  <li>D</li>
  <li>E</li>
  <li>F</li>
  <li>G</li>
  <li id="hello">H
      <div id="hidden-div" style="display:none;">
          This should be displayed after click<br><br>
          And the scroll should come to this screen.
       </div>
    </li>
Run Code Online (Sandbox Code Playgroud)

shi*_*obi 5

在显示/展开隐藏的div之后,调用元素的scrollIntoView函数li这不需要jQuery.

function showIt(elementId) {
    var el = document.getElementById(elementId);
    el.scrollIntoView(true);
}
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅https://developer.mozilla.org/en-US/docs/Web/API/Element.scrollIntoView