Skip to content

Instantly share code, notes, and snippets.

@oirodolfo
Forked from NoamELB/updateHeightRAF.js
Created November 29, 2018 21:18
Show Gist options
  • Select an option

  • Save oirodolfo/a55bfc6f136f12d7a4a611e43c190340 to your computer and use it in GitHub Desktop.

Select an option

Save oirodolfo/a55bfc6f136f12d7a4a611e43c190340 to your computer and use it in GitHub Desktop.
update height with request animation frame
updateHeight(isOpen) {
this.lastRAF && cancelAnimationFrame(this.lastRAF);
if (isOpen) {
this.lastRAF = requestAnimationFrame(() => {
// read:
const height =`${this.contentEl.scrollHeight}px`;
this.lastRAF = requestAnimationFrame(() => {
// write in a different frame:
this.containerEl.style.height = height;
this.lastRAF = null;
});
});
} else {
this.containerEl.style.height = '0px';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment