Element.scrollIntoView not working outside of a timeout in Chrome

2.3k views Asked by At

I have a really strange problem using scrollIntoView. In and Safari and Chrome the code works when I open a new window or tab, but does not work when I push the Refresh button. In Firefox it always works. When I use a setTimeout() the Refresh button triggers a scrolling in all browsers like this:

setTimeout( function () {scrollToSelection();}, 1);

scrollToSelection = function() {
  var element = document.querySelector('p');
  element.scrollIntoView();
};
p {
  margin-top: 1000px;
}
<!DOCTYPE html>
<html>

<head></head>

<body onload="scrollToSelection()">
  <p>Some text to see
  </p>
</body>

</html>

Any ideas what might be causing this?

0

There are 0 answers