score:0

Accepted answer

call it on the overflowing element itself, rather than on the window. htmlelements have a similar function just called scroll().

document.queryselector("button").addeventlistener("click", function(){
  let target = document.getelementbyid("scroll");
  document.queryselector("div").scroll({
    top: target.offsettop,
    behavior: 'smooth'
  });
});
html, body {margin: 0}

div > p
{
  height: 100px;
}

div
{
  height: 100vh;
  overflow: auto;
}

#scroll
{
  height: 240px;
  background: red;
}
<div>
  <button>scroll</button>
  <p>hello</p>
  <p>hello</p>
  <p>hello</p>
  <p>hello</p>
  <p>hello</p>
  <p>hello</p>
  <p>hello</p>
  <p>hello</p>
  <p>hello</p>
  <p>hello</p>
  <p>hello</p>
  <p id="scroll">scroll to me!</p>
</div>


Related Query

More Query from same tag