How can I scroll to the bottom of the desired div? Right now, it will add the height value to the scrollTop which will made the scroll to scroll to only that specific height. I want it to scroll down to the last of the 5th div. There are 9 h1, so I want it to scroll upto the bottom of the 5th h1
const scrollbtn = document.getElementById('scrl');
var objDiv = document.querySelectorAll('.each');
const mycontainer = document.querySelector('.container');
scrollbtn.addEventListener('click', () => {
const itemHeight = objDiv[5].scrollHeight;
mycontainer.scrollTop += itemHeight;
})
body {
background: tomato;
scroll-behavior: smooth;
}
.container {
max-height: 200px;
overflow-y: scroll;
background: white;
scroll-behavior: smooth;
}
.each {
padding-top: 10px;
background: lightblue;
}
<div class="container">
<h1 class="each">Test</h1>
<h1 class="each">Test</h1>
<h1 class="each">Test</h1>
<h1 class="each">Test</h1>
<h1 class="each">Test</h1>
<h1 class="each">Test</h1>
<h1 class="each">Test</h1>
<h1 class="each">Test</h1>
<h1 class="each">Test</h1>
</div>
<button id="scrl">
scroll
</button>