Skip to content

Instantly share code, notes, and snippets.

@danielhaim1
Last active March 19, 2023 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielhaim1/e8aec63aebf28d992837937cbc53b2f3 to your computer and use it in GitHub Desktop.
Save danielhaim1/e8aec63aebf28d992837937cbc53b2f3 to your computer and use it in GitHub Desktop.
The resizeIframe() function has been updated to use ES6 syntax and includes optimization with requestAnimationFrame() to reduce layout thrashing.
const buffer = 20; // scroll bar buffer
const iframe = document.getElementById('ifm');
const pageY = elem => elem.offsetParent ? (elem.offsetTop + pageY(elem.offsetParent)) : elem.offsetTop;
const resizeIframe = () => {
window.requestAnimationFrame(() => {
const height = document.documentElement.clientHeight;
const iframeTop = pageY(document.getElementById('ifm'));
const heightDiff = height - iframeTop + buffer;
const newHeight = (heightDiff < 0) ? 0 : heightDiff;
document.getElementById('ifm').style.height = `${newHeight}px`;
});
};
if (iframe.attachEvent) {
iframe.attachEvent("onload", resizeIframe);
} else {
iframe.onload = resizeIframe;
}
window.onresize = resizeIframe;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment