Skip to content

Instantly share code, notes, and snippets.

@LasaleFamine
Last active July 18, 2018 10:40
Show Gist options
  • Select an option

  • Save LasaleFamine/ab1696a3fa049bccfcd9bcda1d7a2e93 to your computer and use it in GitHub Desktop.

Select an option

Save LasaleFamine/ab1696a3fa049bccfcd9bcda1d7a2e93 to your computer and use it in GitHub Desktop.
function onElementHeightChange(elm, callback) {
let lastHeight = elm.clientHeight;
let lastScrollY = window.pageYOffset;
let newHeight;
let newScrollY;
(function run(){
newHeight = elm.clientHeight;
if(lastHeight !== newHeight) {
callback();
}
lastHeight = newHeight;
if (elm.onElementHeightChangeTimer) {
clearTimeout(elm.onElementHeightChangeTimer);
}
elm.onElementHeightChangeTimer = setTimeout(run, 200);
})();
}
window.onload = () => {
const STATE = {
offset: 0
};
const initAutoResize = addOffset => {
if (parent.postMessage) {
parent.postMessage(document.body.offsetHeight, '*');
}
};
const resizeCallFromParent = event => {
if (event.data === 'resize') {
initAutoResize();
}
};
window.addEventListener('message', resizeCallFromParent);
onElementHeightChange(document.body, initAutoResize);
initAutoResize();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment