Last active
January 23, 2017 12:26
-
-
Save iftee/a788d2c837d952744902f651d18e6051 to your computer and use it in GitHub Desktop.
Responsive YouTube embed for WordPress
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
( function( $ ) { | |
'use strict'; | |
// We'll assume that the post content (and the video) will be inside a wrapper with class .post-content | |
// and the video aspect ratio is 16:9 | |
$( window ).on( 'load resize', function() { | |
// Take the width of the wrapper | |
var contentWidth = $( '.post-content' ).width(); | |
// Resize all YouTube iframes according to the wrapper size with 16:9 aspect ratio | |
$( '.post-content iframe[src*="https://www.youtube.com"]' ).each( function() { | |
$( this ).width( contentWidth ); | |
$( this ).height( contentWidth * 9 / 16 ); | |
} ); | |
} ); | |
} ) ( jQuery ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment