Forked from OrganicPanda/hacky-scrollbar-resize-listener.js
Created
February 23, 2017 08:36
-
-
Save codemilli/b1188e132a0aae000ce9750dd97cda4f to your computer and use it in GitHub Desktop.
A sham that will throw a window resize event even when scrollbars are added/removed (this is not something the standard window resize event does). Tested in IE9+, Chrome & Firefox latest.
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
// Demo: http://jsfiddle.net/pFaSx/ | |
// Create an invisible iframe | |
var iframe = document.createElement('iframe'); | |
iframe.id = "hacky-scrollbar-resize-listener"; | |
iframe.style.cssText = 'height: 0; background-color: transparent; margin: 0; padding: 0; overflow: hidden; border-width: 0; position: absolute; width: 100%;'; | |
// Register our event when the iframe loads | |
iframe.onload = function() { | |
// The trick here is that because this iframe has 100% width | |
// it should fire a window resize event when anything causes it to | |
// resize (even scrollbars on the outer document) | |
iframe.contentWindow.addEventListener('resize', function() { | |
try { | |
var evt = document.createEvent('UIEvents'); | |
evt.initUIEvent('resize', true, false, window, 0); | |
window.dispatchEvent(evt); | |
} catch(e) {} | |
}); | |
}; | |
// Stick the iframe somewhere out of the way | |
document.body.appendChild(iframe); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment