Created
January 29, 2013 22:27
-
-
Save dcatkins40/4668564 to your computer and use it in GitHub Desktop.
Without wrapping an iFrame in a second div container, this code will find all iFrames that you select and fit them to their containers. Great for dynamic content.
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
| jQuery(window).load(function() { | |
| var $allVideos = jQuery("iframe[src^='http://www.youtube.com']"); //Find all embedded iframes from YouTube and load them into an array | |
| var $fluidElement = jQuery("Fluid_Width_Element").width(); | |
| $allVideos.each(function() { | |
| jQuery(this).data('aspectRatio', this.height / this.width).removeAttr('height').removeAttr('width'); //Find the aspect ratio, store it, and remove the height and width attributes | |
| var $aspectRatio = jQuery(this).data('aspectRatio'); | |
| jQuery(this).width($fluidElement).height($fluidElement * $aspectRatio); //Change the height and width of each iframe to fit the container | |
| }) | |
| jQuery(window).resize(function() { | |
| var $newWidth = $fluidElement; //When the window is resized, store the new width | |
| $allVideos.each(function() { | |
| var $element = jQuery(this); | |
| $element.width($newWidth).height($newWidth * $element.data('aspectRatio')); //Change the height and width of each iframe to fit the container every time the window is resized | |
| }) | |
| }) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment