Last active
April 7, 2016 01:52
-
-
Save dylanvalade/b2ba4eaa99ae7968cfd8 to your computer and use it in GitHub Desktop.
jQuery Responsive iFrames
This file contains 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
// Make all iframes responsive | |
// Handle video embeds with aspect ratio | |
// Input: void | |
// Return: void | |
// Dependencies: jQuery | |
// Call responsiveIframes() in $( document ).ready() or $( window ).load after any DOM manipulation | |
function responsiveIframes () { | |
// Handle iframes | |
if( $('iframe').length ) { | |
// 4:3 Video Aspect Ratio = 0.75 | |
// 16:9 Video Aspect Ratio = 0.5625 | |
var aspectRatio = 0.5625; | |
$('iframe').each(function(item) { | |
// Get frame source attribute | |
var frameSource = $(this).attr('src'); | |
// Set iframe width to 100% | |
$(this).attr('width', '100%'); | |
// Hide overflowed content | |
$(this).attr('style', 'overflow: hidden;'); | |
// Handle embedded video if the source includes a video domain | |
if( -1 !== frameSource.indexOf('wistia') || | |
-1 !== frameSource.indexOf('vimeo') || | |
-1 !== frameSource.indexOf('youtube') ) { | |
// Set fixed height using appropriate aspect ratio | |
$(this).attr('height', $(this).innerWidth() * aspectRatio ); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you have extras like share buttons within the embed which you can do with Wistia, it changes the aspect ratio so you'll need to do a little custom math to support videos that include extras outside the bounds of the video media.