Skip to content

Instantly share code, notes, and snippets.

@kohlmannj
Created December 8, 2014 19:13
Show Gist options
  • Save kohlmannj/aef0b2df698727b0ea48 to your computer and use it in GitHub Desktop.
Save kohlmannj/aef0b2df698727b0ea48 to your computer and use it in GitHub Desktop.
jQuery Plugin: iframe Scaler (transforms iframes to fit inside their containing elements)
(function($) {
$.fn.iframeScale = function() {
this.each(function() {
var $iframe = $(this).find("iframe");
var scaleFactor = $(this).width() / $iframe.width();
$iframe.css({
webkitTransform: "scale(" + scaleFactor + ")",
mozTransform: "scale(" + scaleFactor + ")",
msTransform: "scale(" + scaleFactor + ")",
transform: "scale(" + scaleFactor + ")"
});
});
return this;
};
}( jQuery ));
// Sample Usage:
//
// 1. Select containing elements and call the plugin on page load:
// $(".prototype").iframeScale();
//
// 2. Call the plugin on window resize to support fluid width / proportional height containers:
//
// $(window).on("resize", function() {
// $(".prototype").iframeScale();
// });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment