Skip to content

Instantly share code, notes, and snippets.

@ourmaninamsterdam
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ourmaninamsterdam/b7c9194c73bdbbdba09d to your computer and use it in GitHub Desktop.
Save ourmaninamsterdam/b7c9194c73bdbbdba09d to your computer and use it in GitHub Desktop.
Iframe resizer (using jQuery)
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery'],function (jQuery) {
return (root.iframeResizer = factory(jQuery));
});
} else {
root.iframeResizer = factory(root.jQuery);
}
}(this, function ($) {
return function(messageId, minPageHeight, $animatedElements) {
var timer;
minPageHeight = minPageHeight || 250;
return {
start : function() {
if(!window.postMessage) return;
var currentHeight = 0,
$body = $('body');
$('html').css('overflow','hidden');
timer = setInterval(function() {
var documentHeight = Math.max($body.outerHeight(true), minPageHeight);
if(documentHeight !== currentHeight && !$animatedElements.filter(':animated').length) {
window.parent.postMessage(messageId + (documentHeight), '*');
currentHeight = documentHeight;
}
}, 200);
},
stop: function() {
clearInterval(timer);
}
}
}
}));
@ourmaninamsterdam
Copy link
Author

Usage

var iframeResizer = require('iframe-resizer');

// To start
iframeResizer('MASRESIZE', 450, arrayOfjQueryEls).start();

// To stop
iframeResizer.stop();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment