Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save super-dog-human/b9648bb00bf0d0831c530db1052f36b9 to your computer and use it in GitHub Desktop.
Save super-dog-human/b9648bb00bf0d0831c530db1052f36b9 to your computer and use it in GitHub Desktop.
scroll in requestAnimationFrame is not working at chrome

In Chrome 87, scrolling with "scroll-behavior: smooth" is not working from requestAnimationFrame.

CodePen: https://codepen.io/superdoghuman/pen/ExgzjXP

<div id="parent">
  <div class="child">1</div>
  <div class="child">2</div>
  <div class="child">3</div>
  <div class="child">4</div>
  <div class="child">5</div>
</div>
  
<button id="scrollButton">
  start scrolling
</button>
#parent {
  scroll-behavior: smooth; /* auto is working even chrome.*/
  overflow-x: scroll;
  display: flex;
  flex-direction: row;
  align-items: center;
  background-color: gray;
  width: 800px;
  height: 200px;
}

.child {
  background-color: darkgray;
  margin: 20px;
  text-align: center;
  min-width: 300px;
  min-height: 150px;
}

#scrollButton {
  margin-top: 50px;
  width: 150px;
  height:100px;
  font-size: 1rem;
}
const parentElement = document.getElementById("parent");
const scrollFunc = document.getElementById("scrollButton");
scrollFunc.onclick = () => {
  animate();
}

function animate() {
  parentElement.scrollLeft += 10;
  requestAnimationFrame(animate);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment