Last active
March 19, 2023 22:20
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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