Skip to content

Instantly share code, notes, and snippets.

@luanvuhlu
Created January 4, 2017 10:01
Show Gist options
  • Save luanvuhlu/86ae0810d719169c9f0ba60e83e52b10 to your computer and use it in GitHub Desktop.
Save luanvuhlu/86ae0810d719169c9f0ba60e83e52b10 to your computer and use it in GitHub Desktop.
$.event.special.widthChanged = {
remove : function() {
$(this).children('iframe.width-changed').remove();
},
add : function() {
var ss = document.createElement('style');
ss.type = "text/css";
ss.innerHTML = "iframe.width-changed {width: 100%;display: block;border: 0;height: 0;margin: 0;}";
document.body.appendChild(ss);
var elm = $(this);
var iframe = elm.children('iframe.width-changed');
if (!iframe.length) {
iframe = $('<iframe/>').addClass('width-changed').prependTo(this);
}
var oldWidth = elm.width();
function elmResized() {
var width = elm.width();
if (oldWidth != width) {
elm.trigger('widthChanged', [ width, oldWidth ]);
oldWidth = width;
}
}
var timer = 0;
var ielm = iframe[0];
(ielm.contentWindow || ielm).onresize = function() {
clearTimeout(timer);
timer = setTimeout(elmResized, 20);
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment