Skip to content

Instantly share code, notes, and snippets.

@benfoster
Created February 8, 2014 19:48
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 benfoster/8889154 to your computer and use it in GitHub Desktop.
Save benfoster/8889154 to your computer and use it in GitHub Desktop.
jQuery Fitframe
/*!
* jQuery fitframe plugin
* Further changes, comments: @benfosterdev
* Licensed under the MIT license
*/
;(function ( $, window, document, undefined ) {
var pluginName = 'fitframe',
defaults = {
fitHeight: false
},
ratioKey = 'fitframe-ratio';
function Fitframe(container, options) {
this.container = $(container);
this.options = $.extend({}, defaults, options);
this._defaults = defaults;
this._name = pluginName;
this.init();
}
Fitframe.prototype.resize = function() {
var containerWidth = this.container.width(),
containerHeight = this.container.height(),
fitHeight = this.options.fitHeight;
this.container.find('iframe').each(function() {
var $e = $(this),
ratio = $e.data(ratioKey),
newHeight = containerWidth * ratio;
if (fitHeight && (newHeight > containerHeight)) {
// the height overlaps the container so scale the width instead
$e.height(containerHeight)
.width(containerHeight / ratio);
} else {
// scale the height
$e.height(containerWidth * ratio)
.width('100%');
}
});
};
Fitframe.prototype.init = function() {
this.container.find('iframe').each(function() {
var $e = $(this);
$e.data(ratioKey, ($e.attr('height') / $e.attr('width')).toPrecision(4));
});
var self = this, t;
// debounced resizing
window.onresize = function () {
clearTimeout(t);
t = setTimeout(function () {
self.resize();
}, 100);
};
this.resize();
};
// A really lightweight plugin wrapper around the constructor
// preventing against multiple instantiations
$.fn[pluginName] = function(options) {
return this.each(function() {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName, new Fitframe(this, options));
}
});
}
}( jQuery, window, document ));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment