简体   繁体   中英

Pure Javascript - Click and scroll to Div inside div

I have the following HTML, with scrollbar:

<div id="parent">
      <div id="child1">Something</div>
      <div id="child2">Something</div>
      <div id="child3">Something</div>
      <div id="child4">Something</div>
      <div id="child5">Something</div>
      <div id="child6">Something</div>
      <div id="child7">Something</div>
      <div id="child8">Something</div>
      <div id="child9">Something</div>   
</div>

My question is this:

How can I, in pure javascript, position the scroll of the div 'parent' in one of the childs?

You can use the scrollIntoView function of JavaScript like this:

document.getElementById('child9').scrollIntoView()

Try it online

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM