Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best time to update scroll position to minimize visual changes #128

Open
CodeWithOz opened this issue Feb 8, 2022 · 2 comments
Open

Best time to update scroll position to minimize visual changes #128

CodeWithOz opened this issue Feb 8, 2022 · 2 comments

Comments

@CodeWithOz
Copy link

I have a scenario where I want to render a list at a certain scroll position that's not 0. If I update scroll after rendering the list, I can see a brief period where the list is at scrollTop = 0, before it changes to the new scrollTop value. This corresponds to code like this:

const listHtml = '<ul>...</ul>'
requestAnimationFrame(() => {
  listContainer.innerHTML = listHtml;
  setTimeout(() => listContainer.scrollTop = nonZeroValue, 0);
});

This brief period looks like a flash of content because it's so quick. To prevent it, I set the scroll position directly inside rAF immediately after rendering the list items. So that code above became this:

const listHtml = '<ul>...</ul>'
requestAnimationFrame(() => {
  listContainer.innerHTML = listHtml;
  listContainer.scrollTop = nonZeroValue;
});

This took away the flash, the scroll position is set immediately the items are rendered. However, this significantly slows down the time it takes to actually paint the rendered list items.

So my question is what's the right time to update scroll position to both avoid the flash and minimize the delays to layout/painting?

@wilsonpage
Copy link
Owner

wilsonpage commented Feb 8, 2022 via email

@CodeWithOz
Copy link
Author

Thanks for the suggestion! Yeah using opacity like that won't be ideal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants