Last active
July 18, 2018 10:40
-
-
Save LasaleFamine/ab1696a3fa049bccfcd9bcda1d7a2e93 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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