Last active
March 10, 2016 12:58
-
-
Save modcab/fd39b0c8e4e944879fcc to your computer and use it in GitHub Desktop.
jQuery - Make video in iframe responsive
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
// Makes iframes inside of a node body responsive. | |
Drupal.behaviors.responsive_iframes = { | |
attach: function (context) { | |
$(document).ready(function() { | |
// Iframe needs to have the class .responsive. | |
$('.field-name-body iframe').each(function(index, value) { | |
// Height and width both need int values. | |
var height = parseInt($(this).attr('height')) || null; | |
var width = parseInt($(this).attr('width')) || null; | |
if ((height !== null) && (width !== null)) { | |
var ratio = (height/width) * 100; | |
// Adding a wrapper around the iframe with the ratio in the iframe. | |
$(this).wrap("<div style=\"position: relative;padding-bottom:" + ratio + "%\"></div>"); | |
// Modifying css for the iframe so it's responsive. | |
$(this).css({ | |
position: 'absolute', | |
top: 0, | |
left: 0, | |
width: "100%", | |
height: "100%" | |
}); | |
} | |
}); | |
}); | |
} | |
} | |
})(jQuery, Drupal); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment